Solved ms dos command rename files adding characters

June 24, 2016 at 06:15:39
Specs: Windows 7
Having trouble getting the correct command format. We need to add a leading zero to a batch of files in a directory on a regular basis. I cannot download and install a software The operating system is Windows- The length of the filenames varies. i.e. 000165-CityName1.pdf to 0000165-CityName1.pdf 000166-CityNameLonger2.pdf to 0000166-CityNameLonger2.pdf etc. Looking for a rename command that would work, can someone please suggest one, it would be appreciated. I have tried a half dozen without success.
Thank you Russ

message edited by techexpressinc


See More: ms dos command rename files adding characters

Report •

✔ Best Answer
June 24, 2016 at 11:11:02
Alternate solution:

1) Save this as a batch file in the folder in question:

for /f %%a in ('dir /b/a-d') do if "%%~Na" NEQ "%%~N0" ren "%%a" "0%%~Na"

2) Run the batch file.

How To Ask Questions The Smart Way

message edited by Razor2.3



#1
June 24, 2016 at 07:20:57
What, are you going for a certain character length for the number part, or are you looking to just arbitrarily add a '0' to every file in a directory?

How To Ask Questions The Smart Way


Report •

#2
June 24, 2016 at 07:30:28
I am looking to just arbitrarily add a '0' to every file in a directory.

Report •

#3
June 24, 2016 at 07:54:55
Step 1 - Click Windows-Icon get the run dialogue box.

Step 2 - Type "Powershell", select "Windows Powershell" this opens a window in MS-Dos Command layout.

Step 3 - Copy this into clipboard:
Get-ChildItem -Path "C:\temp\cityfiles\" |
Rename-Item -NewName {$_.BaseName.insert(0,'0') + $_.Extension}

Step 4 - Go to the window open in Step 2, and right click the mouse. Then click enter. The renames should take place.

Close windows.


Report •

Related Solutions

#4
June 24, 2016 at 07:55:42
Solved!
Thanks for the help
Russ

Report •

#5
June 24, 2016 at 11:11:02
✔ Best Answer
Alternate solution:

1) Save this as a batch file in the folder in question:

for /f %%a in ('dir /b/a-d') do if "%%~Na" NEQ "%%~N0" ren "%%a" "0%%~Na"

2) Run the batch file.

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

Ask Question