File Prefix Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

File Prefix Help

#1 Post by born2achieve » 03 Jun 2015 18:21

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: File Prefix Help

#2 Post by foxidrive » 03 Jun 2015 21:04

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")

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Prefix Help

#3 Post by Aacini » 03 Jun 2015 21:50

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

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: File Prefix Help

#4 Post by born2achieve » 04 Jun 2015 08:19

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: File Prefix Help

#5 Post by foxidrive » 04 Jun 2015 08:34

You can use find /v to filter them out.

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

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Prefix Help

#6 Post by Aacini » 04 Jun 2015 09:56

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

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: File Prefix Help

#7 Post by born2achieve » 04 Jun 2015 13:13

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")

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: File Prefix Help

#8 Post by born2achieve » 04 Jun 2015 13:21

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

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Prefix Help

#9 Post by Aacini » 04 Jun 2015 18:03

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

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: File Prefix Help

#10 Post by Aacini » 04 Jun 2015 18:15

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

born2achieve
Posts: 51
Joined: 16 Nov 2014 20:28

Re: File Prefix Help

#11 Post by born2achieve » 04 Jun 2015 18:21

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/

Post Reply