Page 1 of 1

File Prefix Help

Posted: 03 Jun 2015 18:21
by born2achieve
Hi,

I need to add the prefix to the files in specific directory. For this i need to input the directory name.

I need to add the prefix "Sample_" to all the txt files in the specified directory

Sample code i tried:

Code: Select all

FOR /f "delims=" %%F IN ("D:\Sample\*.txt")  DO (RENAME "%%F" "Sample_%%F")


but it's not working. any suggestion please

Re: File Prefix Help

Posted: 03 Jun 2015 21:04
by foxidrive
It could be a couple of things: try this

Code: Select all

FOR /f "usebackq delims=" %%F IN ("D:\Sample\*.txt")  DO (RENAME "%%F" "Sample_%%F")


Or this:

Code: Select all

FOR /f "usebackq delims=" %%F IN ("D:\Sample\*.txt")  DO (RENAME "%%F" "Sample_%%~nxF")

Re: File Prefix Help

Posted: 03 Jun 2015 21:50
by Aacini
There are two types of FOR command. The plain FOR on files:

Code: Select all

FOR %%F IN (files set) DO command ...

... and the FOR /F on the result of a command:

Code: Select all

FOR /F "options" %%F IN ('command enclosed in apostrophes') DO command ...

..., but your FOR is no one of them!

Code: Select all

FOR /f "delims=" %%F IN ("D:\Sample\*.txt")  DO (RENAME "%%F" "Sample_%%F")

If you want to use a FOR /F command, insert a DIR command enclosed in apostrophes this way:

Code: Select all

FOR /f "delims=" %%F IN ('dir /B "D:\Sample\*.txt"')  DO (RENAME "%%F" "Sample_%%F")

If you want to use a plain FOR (that is always prefered), remove the /F and the options:

Code: Select all

FOR %%F IN ("D:\Sample\*.txt")  DO (RENAME "%%F" "Sample_%%F")

You must also use just the name of the file in the second part of RENAME command as foxidrive suggested, that is: "Sample_%%~nxF .

Antonio

Re: File Prefix Help

Posted: 04 Jun 2015 08:19
by born2achieve
Thanks a lot guys. it worked. is there any way to check if the already starts with "Sample_" and skip in that case adding the prefix.

Thanks

Re: File Prefix Help

Posted: 04 Jun 2015 08:34
by foxidrive
You can use find /v to filter them out.

Which format of the commands that were suggested are you using?

Re: File Prefix Help

Posted: 04 Jun 2015 09:56
by Aacini
born2achieve wrote:Thanks a lot guys. it worked. is there any way to check if the already starts with "Sample_" and skip in that case adding the prefix.

Thanks


Code: Select all

@echo off
setlocal EnableDelayedExpansion

FOR %%F IN ("D:\Sample\*.txt")  DO (
   SET "NAME=%%~nF"
   IF /I "!NAME:~0,7!" NEQ "Sample_" RENAME "%%F" "Sample_%%~nxF"
)

Antonio

Re: File Prefix Help

Posted: 04 Jun 2015 13:13
by born2achieve
Hi Fox,

Below the command working for me that provided prior to my previous post

Code: Select all

FOR /f "delims=" %%F IN ('dir /B "D:\Sample\*.txt"')  DO (RENAME "%%F" "Sample_%%F")

Re: File Prefix Help

Posted: 04 Jun 2015 13:21
by born2achieve
Hi Aacini,

I tried your script and it doesn't cheking the file same already starts with "Sample_". Waht i did was, i tried to run the ba file first time and it sucessfully Added the prefix as 'Sample_". Then again i tried to run and it again added another prefix on top of it which is not supposed to be.

like "Sample_Sample_'.

Any suggestion please

Re: File Prefix Help

Posted: 04 Jun 2015 18:03
by Aacini
It works here!

Code: Select all

C:\ dir C:\Sample
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Sample

04/06/2015  06:53 p. m.    <DIR>          .
04/06/2015  06:53 p. m.    <DIR>          ..
04/06/2015  06:52 p. m.                 6 File One.txt
04/06/2015  06:53 p. m.                 6 Other file.txt
04/06/2015  06:53 p. m.                 6 Sample_file.txt
04/06/2015  06:53 p. m.                 6 Sample_One.txt
               4 archivos             24 bytes
               2 dirs  423,192,457,216 bytes libres

C:\ type test.bat
@echo off
setlocal EnableDelayedExpansion

FOR /f "delims=" %%F IN ('dir /B "C:\Sample\*.txt"')  DO (
   SET "NAME=%%~nF"
   IF /I "!NAME:~0,7!" NEQ "Sample_" RENAME "%%F" "Sample_%%~nxF"
)

C:\ test

C:\ dir C:\Sample
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Sample

04/06/2015  06:58 p. m.    <DIR>          .
04/06/2015  06:58 p. m.    <DIR>          ..
04/06/2015  06:52 p. m.                 6 Sample_File One.txt
04/06/2015  06:53 p. m.                 6 Sample_file.txt
04/06/2015  06:53 p. m.                 6 Sample_One.txt
04/06/2015  06:53 p. m.                 6 Sample_Other file.txt
               4 archivos             24 bytes
               2 dirs  423,192,457,216 bytes libres

C:\ test

C:\ dir C:\Sample
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 0895-160E

 Directorio de C:\Sample

04/06/2015  06:58 p. m.    <DIR>          .
04/06/2015  06:58 p. m.    <DIR>          ..
04/06/2015  06:52 p. m.                 6 Sample_File One.txt
04/06/2015  06:53 p. m.                 6 Sample_file.txt
04/06/2015  06:53 p. m.                 6 Sample_One.txt
04/06/2015  06:53 p. m.                 6 Sample_Other file.txt
               4 archivos             24 bytes
               2 dirs  423,192,457,216 bytes libres

Please, post an example that show your problem...

Antonio

Re: File Prefix Help

Posted: 04 Jun 2015 18:15
by Aacini
Ops! There is a problem when the files are placed on a drive different than the one where the Batch file is located! Change the code by this one:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

cd /D D:\Sample
FOR /f "delims=" %%F IN ('dir /B *.txt')  DO (
   SET "NAME=%%~nF"
   IF /I "!NAME:~0,7!" NEQ "Sample_" RENAME "%%F" "Sample_%%~nxF"
)

Please, inform the result...

Antonio

Re: File Prefix Help

Posted: 04 Jun 2015 18:21
by born2achieve
Hi Anto,

Thanks for your reply and i cannot use cd /D D:\Sample. because i would be using the remote path where i have access.

for example, i would be using the network path like below. Sorry that i didn't communicate about this. i thought of replacing the local path with n/w path would work. Any suggestion please

//mynetwork/mysample/Files/