New
#21
Hi,
This was kind of a test script to get the correct rename function... we have since moved onto the finished article at post #17 - Remove last seven characters from multiple filenames?
Regards,
JDobbsy1987
I saw everyone enthusiastically discussing about the scripting language and batch file programming for removing the last seven characters from a filename.
Another simple alternative is to download the free Bulk Rename Utility No Installer version (zipped file), Unzip the files to a USB flash drive, and use it without installing on your office system.
Thanks, Rraod. I'll try that at home, but at work I'm not allowed even to run unapproved programs, whether I install them on the system or not. I'm sure the makers of Bulk Rename Utility are honest and responsible, but if by some fluke a bad guy had cracked their site and infected their downloads with a virus, it'd mean my job, at a minimum. Security is a huge issue at my company.
Also, as JDobbsy1987 suggests above, this is a valuable learning opportunity for me. There's value, at least for me, in learning how to write simple scripts like this, to handle little everyday computer tasks.
OK, I've tried JDobbsy1987's two batch files, and they appear to be having trouble with the spaces in my filenames. I put a file named <EY 000000012768048.PDF> into a Temp directory and ran RunMe.bat. It should have renamed the file <EY 00000001.PDF>, but here's what happened:
e:\Temp>dir /b | find "pdf" /i 1>temp.log(Yes, I found out how to redirect a batch file's output to a log file!)
e:\Temp>set tempvar=
e:\Temp>for /F "tokens=1-2 delims=." %A in (temp.log) do (rename_file.bat %A %B )
e:\Temp>(rename_file.bat EY 000000012768048 pdf )
e:\Temp>set fName=EY
e:\Temp>ren EY.000000012768048 .000000012768048
The system cannot find the file specified.
I experimented with putting variables in quotation marks, but nothing seemed to work. Then I remembered, RTFM! C:\>FOR /? informs me that the "usebackq" option allows one to use double quotation marks on filenames in "file-set".
Unfortunately, that gets me only so far, because in our setup "file-set" is <temp.log>, not the filename with the troublesome space--which is a string within <temp.log>. So we need to put double quotes around the string in <temp.log>, either when it's written to that file, or when it's passed to <ReName_File.bat>.
A little more searching on the web took me to this thread (on a site that inflicts annoying pop-overs, or whatever they're called), where a helpful person points out the following:
So I edited <RunMe.bat> to put double quotes around the parameters that get fed to <ReName_File.bat>, thus:
call gets expanded to:
:prtpath C: Documents and Settings All Users
which is correctly mapped to:
%1 = C:
%2 = Documents
%3 = and
%4 = Settings
%5 = All
%6 = Users The trick is to double quote the strings that may contain spaces:
for /f "usebackq tokens=1-7* delims=\" %%a in (`echo %myvar%`) do call
:prtpath "%%a" "%%b" "%%c" "%%d" "%%e" "%%f" "%%g"
(I also deleted the second "/f" that JDobbsy1987 had in his "FOR" clause, on the suspicion that it was unnecessary and had been left in by accident. The batch seems to run OK without it.)Code:dir /b | find "pdf" /i > temp.log set tempvar= for /f "tokens=1-2 delims=." %%A in (temp.log) do ( rename_file.bat "%%A" "%%B" ) del temp.log
I don't think I need quotes around %%B, but they don't seem to do any harm. The quotes around %%A added a digit to either end of string %A, so I edited <ReName_File.bat> to account for that, by changing JDobbsy1987's ~0,-7 to ~1,-8:
Only that caused a syntax error, which I don't understand. Changing the ~1 back to ~0 (but leaving -8) eliminates the error, and it seems to work, although I don't know why.Code:set fName=%1 ren %fName%.%2 %fName:~1,-8%.%2
I initially added "usebackq" to the FOR options, but I accidentally deleted it, and the script ran just as well without it, so I guess it wasn't needed.
With those modifications, the script seems to work exactly right, except that it doesn't delete temp.log when it's finished. That's a very minor thing, and I'll figure out what's causing it when I have time--unless JDobbsy1987 beats me to it.
Just as an academic matter, I don't understand what's going on with the quotation marks I introduced. My log file reports the following:
<RunMe.bat> is putting quotation marks around the filename and the extension separately, with the dot delimiter in between, instead of around the whole filename (i.e., "filename"."ext" instead of "filename.ext"). I think I understand why it does that, but I don't know how to make it do the other, which I think would be better. Only it doesn't seem to matter, because I'm getting the results I want: the REN command seems to be ignoring the quotes around the dot (i.e., at the end of the filename and the beginning of the extension), even after the one at the end of the filename has been trimmed off, leaving an odd " ; and I don't know why it should do that. These are the mysteries of programming, I suppose. If any of you would care to enlighten me about them, I'll let you ride my bicycle.e:\Temp>dir /b | find "pdf" /i 1>temp.log
e:\Temp>set tempvar=
e:\Temp>for /F "tokens=1-2 delims=." %A in (temp.log) do (rename_file.bat "%A" "%B" )
e:\Temp>(rename_file.bat "EY 000001832768069" "pdf" )
e:\Temp>set fName="EY 000001832768069"
e:\Temp>ren "EY 000001832768069"."pdf" "EY 00000183."pdf"
Thanks again to JDobbsy1987 and all who've chimed in to help.
Hi Jim,
Spaces in file names... hhmmm... here it goes... (keeping to the same principle of 2 batch files)
Make batch file called "RunMe.bat" with the following code:
Now create a batch file called "ReName_File.bat" with the following code:Code:dir /b | find "pdf" /i > temp.log set tempvar= for /f "tokens=1-2 delims=." /f %%A in (temp.log) do ( rename_file.bat "%%A" %%B ) del temp.log
Put those 2 batch files in the folder with your PDF's and double click RunMe.bat (It may not delete the temp.log but thats nothing. once its finished you can just delete that).Code:set fName=%1 set fName=%fName:~0,-1% set fName=%fName:~1% ren "%fName%.%2" "%fName:~0,-7%.%2"
I have tested the above scripts with PDF's in a folder, without spaces, with 1 space and multiple spaces.
/Fingers Crossed
Obviously as this is set to remove the last 7 characters, if you have mixed PDF's in the folder (i.e. some with spaces and some without then it may not work perfect)
e.g.
JDobbsy1987PDF1234567.pdf >> Would Become >> JDobbsy1987PDF.pdf
but
JDobbsy1987PDF123 4567.pdf >> Would Become >> JDobbsy1987PDF1.pdf
This is because the space will also be counted as 1 character.
Regards,
JDobbsy1987
Hello Gurus,
I have seen lot of postings on renaming the filename using batch script (.cmd script or .bat script) None really worked for my scenario though it was same. Any help would be greately appreciated.
Here is my problem. I have several pdf files under "\\servername\C$\Test Scripts" folder that ends with "-en-us" so wanted to stript them off. For ex: "3test-en-us.pdf" to "3test.pdf" and it has to be done recursevely in all sub folders underneath of the above mentioned folder. None seems to be working. And this has to be automated, any code that does this would be greately appreciated.
Pl email me the code if you can to kevnss@textfree.us or reply back would be a great help.
I have a Sourcefolder with following files and file types
ab c xxxxxx.sss
ab c xxxxxx.fff
ab c xxxxxx.ggg
ab c xxxxxx.hhh
where xxxxxx is datenumber code varying from dataset to dataset.
I wish to run a batch script (*.bat) which
1- temporarily changes the path to the SourceFolder directory
2- removes spaces (and the datenumbers) and renames all the files as follows
opqr.sss
opqr.fff
opqr.ggg
opqr.hhh
3- copies the renamed files to a SourceFolder\Dump folder.
4- moves the files from Dump folder to a Destination folder.
I will really appreciate your help.