1.

Solve : rename multiple files?

Answer»

im trying to rename multple files ina folder but keep the existing name and jsut add a word in the front. so for example


test.txt
test2.txt
test3.txt
test4.txt

rename them to...(im just adding cp to the front of the file name

cp test.txt
cp test2.txt
cp test3.txt
cp test4.txt

All of the files, or just some of them?
all of the files sorryCode: [SELECT]@for /f "delims=" %%A in (' dir /a-d /b *.* ^| find /v "%~nx0" ' ) do ren "%%~dpnxA" "cp %%~nxA"
Save this as a .bat file e.g. MyRename.Bat and run it in the folder. It will not rename itself.
Thanks alot it worked perfect. i had one more question can you exaplin what each part is doing? like what does "%~nx0" stand for? i know that the %%A is a varabile for the loop. but im trying to break down the code to understand everything its doing. if you can break it down that would be great. THANK you again



@for /f "delims=" %%A in (' dir /a-d /b *.* ^| find /v "%~nx0" ' ) do @echo ren "%%~dpnxA" "cp %%~nxA"
Quote from: daillest319 on October 24, 2012, 11:32:29 AM

Thanks alot it worked perfect. i had one more question can you exaplin what each part is doing? like what does "%~nx0" stand for? i know that the %%A is a varabile for the loop. but im trying to break down the code to understand everything its doing. if you can break it down that would be great. thank you again



@for /f "delims=" %%A in (' dir /a-d /b *.* ^| find /v "%~nx0" ' ) do @echo ren "%%~dpnxA" "cp %%~nxA"

I see you NOTICED the echo that I left in for debugging.

for /f "delims=" %%A in (dataset)
... the /f switch tells FOR to treat (dataset) as a source of lines to be processed. If dataset is a command enclosed in single quotes then the command is executed and the output is processed.

the command is : dir /a-d /b *.*
... list files, but not directories in bare format

^| find /v "%~nx0"

... PIPE the output of dir through find (the pipe symbol is escaped with a CARET if used in a FOR dataset). The /v switch says to find.exe, "show lines which do not contain the search string".

... the search string for find.exe is %~nx0.

%0 is the batch file name. The ~n and ~x modifiers mean "name" and "extension" so the batch file being run is filtered out of the file list produced by DIR, and thus the batch itself is not renamed.

The other variable modifiers used in the script are ~d ~p, drive and path respectively.

%%~dpnxA is the full drive letter, path, name and extension of each file listed by DIR

%%~nxA is the bare name

"cp %%~nxA" is the new name - that is, the original name and extension, with cp and a space in front of it.

to learn more about DIR. FOR, FIND, etc, type the command followed by /? and read the help.

thank you so much .Quote from: foxidrive on October 24, 2012, 04:28:02 PM
.

HuH ? ?I was going to suggest a simpler method - only then I realised it wasn't a filespec of test*.* but the OP wanted *.* and my method wouldn't have been appropriate due to a bug in for-in-do.

We used to be able to delete posts, but that ability was removed? So I edited it. Now i'll know what the period means foxidrive...

The feature just so you know was restricted due to a few posters that abused it incessantly...making many Topics almost un-intelligible.
Thanx for the clarification.

patio.Quote from: foxidrive on October 24, 2012, 05:44:33 PM
We used to be able to delete posts, but that ability was removed? So I edited it.

I have had to do this more than once. Sometimes I have managed to disguise the situation by hurriedly thinking up some other thing to post, more often I have just put a dot.


Discussion

No Comment Found