Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.
He's playing a game called IRL. Great graphics, *censored* gameplay.
setlocal enabledelayedexpansionfor /f "delims=" %%A in (*.txt) do ( set oldname=%%A set newname=!oldname:_= ! ren "!oldname!" "!newname!" )
@echo offsetlocal enabledelayedexpansiondir /b the*.txt > _txtfiles.txtfor /f "delims=" %%i in (_txtfiles.txt) do (echo %%iset var=%%iFor /F "delims=" %%a in ('echo %%i ^| sed 's/_/ /g') do set var3=%%aecho var =!var!echo var3 =!var3!ren "!var!" "!var3!"rem ren "!var3!" "!var!")
"'sed' is not recognized as an internal or external command,operable program or batch file."
Suppose they don't want to use your weird sounding program?
I can't figure it out...I'd like to rename a number of files, replacing underscores with a space.For example, I'd like to rename "the_first_file.txt" to "the first file.txt" I thought the following would work, but it didn't:RENAME *_*>* *" "*.*Can anyone help a brother out?
I better fess-up on my newbieness; I know less than you think. For the first proposed solution above (thanks to Helpmeh and Salmon Trout), do I just type it into a DOS emulator, or is it more involved?Keep in mind that I want this to go through a few hundred files and make this change to them all (swap an underscore for a space).Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.I tried to identify the elements of the above solution so as to educate myself.Below is a line-by-line explanation. Is my understanding correct?setlocal enabledelayedexpansion - the activity of the command will be kept to a specified folderfor /f "delims=" %%A in (*.txt) do - 'for' is like an "IF" command, but I don't know what /f does. - I don't get the "delims=" either. - Are the percentage signs are wildcards? - I know that *.txt means 'all text files' set oldname=%%A - does this specify the format of the old name? set newname=!oldname:_= ! - this makes sense (I think), it changes the underscore to a space ren "!oldname!" "!newname!" - this renames the file, with the new (underscoreless) nameThank you for your insight and patience,
Ghostdog, there are no instances of multiple underscores, but I would want them replaced by a single space.
Set objFS = CreateObject("Scripting.FileSystemObject")Set regEx = New RegExp ' Create regular expression.regEx.Global=TruestrFolder = WScript.Arguments.Item(0)strOld = WScript.Arguments.Item(1)strNew = WScript.Arguments.Item(2)regEx.Pattern = strOld&"+"Set objFolder = objFS.GetFolder(strFolder)For Each strFile In objFolder.Files strFileName = strFile.Name If objFS.GetExtensionName(strFileName) = "txt" And InStr(strFileName,strOld)>0 Then strFileName = regEx.Replace(strFileName, strNew) strFile.Name = strFileName End If Next
c:\test> cscript //nologo replace.vbs c:\test _ " "
Thank you for your insight and patience.
Learn Sed ( stream editor ). One simple line of code does it all:type _txtfiles.txt | sed 's/_/ /g' > newfiles.txtref:http://gnuwin32.sourceforge.net/packages/sed.htmhttp://www.computerhope.com/unix/used.htmhttp://www.faqs.org/faqs/editor-faq/sed/
bill, will you please shut up about sed.
Bill, will you please shut up about sed.
Who are You? You are not in charge of anything.
I don't see any renaming going on, Bill.
See post #5 in this thread.
Way to edit your post with more useless code.
Bill, if I was new to this forum I'd think you were about 5 years old.
We've given the working code to the OP, he has used it but questions how it works. We (ST and I) have explained the code and the OP has left. No need to bump a dead topic.
Only the original poster can ever benefit from the code.
The correct grammar: "if I were" not "if I was"
We've given the working code to the OP, he has used it but questions how it works.
We (ST and I) have explained the code and the OP has left. No need to bump a dead topic.
GD74, we're having a whack at Billrich. Whose side are you on?
@echo offecho Ready to rename filesecho.pauseecho.echo Renaming files...echo.setlocal enabledelayedexpansionfor /f "delims=" %%A in (*.txt) do ( set oldname=%%A set newname=!oldname:_= ! echo Renaming !oldname! to !newname! ren "!oldname!" "!newname!" )echo.echo All doneecho.pause
@echo ofecho Ready to rename filesecho.pauseecho.echo Renaming files...echo.setlocal enabledelayedexpansionfor /f "delims=" %%A in ( 'dir /b *.txt' ) do ( set oldname=%%A set newname=!oldname:_= ! echo Renaming !oldname! to !newname! ren "!oldname!" "!newname!" )echo.echo All doneecho.pause
To further my understanding, what does the switch b do (/b)?
Now, just to be picky, I'd love to have certain [first] letters capitalized.
@echo offecho Ready to rename filesecho.pauseecho.echo Renaming files...echo.setlocal enabledelayedexpansionfor /f "delims=" %%A in ( 'dir /b *.txt' ) do ( set oldname=%%A set newname=!oldname:_= ! set char1=!newname:~0,1! set rchars=!newname:~1! if "!char1!"=="a" set char1=A if "!char1!"=="b" set char1=B if "!char1!"=="c" set char1=C if "!char1!"=="d" set char1=D if "!char1!"=="e" set char1=E if "!char1!"=="f" set char1=F if "!char1!"=="g" set char1=G if "!char1!"=="h" set char1=H if "!char1!"=="i" set char1=I if "!char1!"=="j" set char1=J if "!char1!"=="k" set char1=K if "!char1!"=="l" set char1=L if "!char1!"=="m" set char1=M if "!char1!"=="n" set char1=N if "!char1!"=="o" set char1=O if "!char1!"=="p" set char1=P if "!char1!"=="q" set char1=Q if "!char1!"=="r" set char1=R if "!char1!"=="s" set char1=S if "!char1!"=="t" set char1=T if "!char1!"=="u" set char1=U if "!char1!"=="v" set char1=V if "!char1!"=="w" set char1=W if "!char1!"=="x" set char1=X if "!char1!"=="y" set char1=Y if "!char1!"=="z" set char1=Z set newname=!char1!!rchars! echo Renaming !oldname! to !newname! ren "!oldname!" "!newname!" )echo.echo All doneecho.pause
luckily the english alphabets only have 26 characters
S:\>dir /b *.txttest_file_002.txttest_file_003.txttest_file_001.txt
Ready to rename filesPress any key to continue . . .Renaming files...Renaming test_file_002.txt to Test file 002.txtRenaming test_file_003.txt to Test file 003.txtRenaming test_file_001.txt to Test file 001.txtAll donePress any key to continue . . .
S:\>dir /b *.txtTest file 002.txtTest file 003.txtTest file 001.txt
If the files are currently as below:file 1 - test one.txtFile 2 - test two.txtfile 3 - test three.txtI would like:File 1 - Test one.txtFile 1 - Test two.txtFile 1 - Test three.txt
set char1=!newname:~0,1!
set rchars=!newname:~1!
the_mad_joker,I like your method.If you don't know the academic answer...just use the pragmatic solution.
I think It shld a bit long waycorrect me if i am wrongFirst make list of files by using :1} dir PLACE UR DIRECTORY HERE\*.txt /b >list.txt2}edit list.txt with notepad3}replace "_" with "space" [without quotes]4}add "ren" be4 every file's name and then the filefor exren the_example.txt the examplesave it as BATCH and apply it
do you know how long that will take? Try doing it with hundreds of files. What about thousands?
Sounds like a manual method. Use Notepad? This guy is a mad joker.ST and Helpmeh used an automatic one liner.
none of it's actions are automated,
My is, I click replace all. That's automation. Look up automation.
B C programmer, That's a good answer. Rather than pursue this any further, let me pick your brain for something I have thought about. It relates to the topic at hand.In any situation like the one in this topic there are any number of scenarios where you might want to replace or change or rename a file or set of files in a group of directories. Perhaps some users have a need to do this on a regular basis. Now is there a way of making an automated program that will help the user do this without the need for the user to have to rewrite and edit a batch file.? In other words, a script that would be fully automated from the viewpoint of the end-user. To that end, I was wondering if there is a way to invoke notepad and make it do search and replace from a script? If not, would a person have to use one of the GNU utilities such as SED?
notepad.exe /F "word_to_find" myfile #find wordnotepad.exe /R "word_to_replace" myfile #find and replace
Just for record, you can also use edlin to "script" editing a file automatically in windows platform.
Works better is you make a patch to edlin.
Set objFS=CreateObject("Scripting.FileSystemObject")Set regEx = New RegExpSet objArgs = WScript.ArgumentsstrPattern = objArgs(0)p = Split(strPattern,"/")strOld = p(1)strNew = p(2)regEx.Pattern = strOldIf p(0) = "s" Then r = 1 'set replace Else r = 0End If If p(UBound(p)) = "g" Then regEx.Global=True 'set global flag Else regEx.Global=FalseEnd If strFile = objArgs(1)Set objFile = objFS.OpenTextFile(strFile)Do Until objFile.AtEndOfLine If r= 1 Then strLine = objFile.ReadLine strLine = regEx.Replace(strLine, strNew) WScript.Echo strLine Else 'strLine = regex.Execute(strLine) WScript.Echo "-->" & strLine End If Loop
C:\test>more fileold new oldblaholdC:\test>cscript //nologo sed.vbs "s/old/new/" filenew new oldblahnewC:\test>cscript //nologo sed.vbs "s/old/new/g" filenew new newblahnewC:\test>sed "s/old/new/" filenew new oldblahnewC:\test>sed "s/old/new/g" filenew new newblahnew
The second part of the problem is that how do I execute this piece of code recursively for all subdirectories. mushutterclicks
C:\>dir /?Displays a list of files and subdirectories in a directory. / /S Displays files in specified directory and all subdirectories. /for /f "delims=" %%a in ('dir /b') do (for /f "delims=" %%a in ('dir /s /b') do (
@echo offsetlocal enabledelayedexpansionrem for /f "delims=" %%a in ('dir /b') do (for /f "delims=" %%a in ('dir /s /b') do (set fileorig=%%aset filenew=!fileorig:_= !ren !fileorig! "!filenew!")
Just throwing this out there: isn't the /s switch supposed to come after the /b switch?
Oookkk. I wasn't sure, as I've always used /b /s and have never encountered problems (except when there are way too many files/folders in one folder).
Check them out. I just did.
Please post the code you used. We can modify from there.The code offered was one line from #1 post in this thread. All of the batch file must be used:Code: [Select]@echo offsetlocal enabledelayedexpansionrem for /f "delims=" %%a in ('dir /b') do (for /f "delims=" %%a in ('dir /s /b') do (set fileorig=%%aset filenew=!fileorig:_= !ren !fileorig! "!filenew!")"Save it in the folder which contains the files you want to change. "Save the above code to a file with .bat extention. For example: namepic.bat. Then type namepic.bat at the command line and press return.C:\>namepic.bat
For /r %%x in (*.jpg) do ren "%%x" *.jpegNotice the /r switch in the FOR clause for scanning the directories recursively.
We have pasted my level of expertise. Contact Salmon Trout directly.The two of you can make the this work. Size and volume of the image files might be part of the problem.Good luckbye
Size and volume of the image files might be part of the problem.
Does the DIR command stop mid-process and exit (not giving an error message), freeze (also not giving an error message), or just give an error message and not do anything?
There are more files in the directory than the command interpreter (COMMAND.COM) can sort. The maximum amount of memory that COMMAND.COM can allocate for a DIR operation is 64 kilobytes minus 512 bytes. Each file displayed by the DIR command requires 22 bytes for its entry, making the total number of files that can be displayed in a sorted directory equal to 2295 files [(65536-512)/22]. # Microsoft MS-DOS 6.22 Standard Edition# Microsoft Windows 95http://support.microsoft.com/kb/100842
The DIR command can be very finicky with huge amounts of files/folders in one root. Does the DIR command stop mid-process and exit (not giving an error message), freeze (also not giving an error message), or just give an error message and not do anything?The only time I've actually messed up the DIR command (when I typed it in properly) was when there were tens of thousands of folders, within one sub folder (I was trying to see the network drive), although the problem could probably be reproduced with a much less amount.
I am testing this with few files only. So no question of having huge number of files.I get this error with the /s option with just 3 files in one directory and it does not even rename the files under the same directory. It works file without the /s option. But then that's not for recursive.The syntax of the command is incorrect.The syntax of the command is incorrect.The syntax of the command is incorrect.Thanks,myshutterclicks
Can you post all code in your script right now? A syntax error means the switches/etc. were not written properly.
Doing DIR /? gives the list of options.It states you give the file name first and follow with the switches.So why not try it the recommended way?Using a feature that is not given in the documentation is not wise, unless you have a reason for it. (So you just want to see what happens?)
Sorry that did not help.I am just shooting in the dark. That is why I am the Geek after dark.You need the /s to get all sub directories. -Right?Well, just move the DIR thing out of the for loop.I have been following this third, but get lost in the stuff. Do you really need recursion? Can you just use iteration? Maybe I missed something.Could you just do DIR *.JPG /B/S >big-list.txtThen have the for loop precess each line in big-list.txtThat way the for loop does not have to do recursion. I guess.
RENAME [drive:][path]filename1 filename2
RENAME [drive:][path]filename1 [drive:][path]filename2
Rename "C:\My Documents\My Pictures\My Meals\Cheeseburger105.jpg" "C:\My Documents\My Pictures\My Meals\Hamburger105.jpg"
Rename "C:\My Documents\My Pictures\My Meals\Cheeseburger105.jpg" "Hamburger105.jpg"
Note that you cannot specify a new drive or path for your destination file.
@echo offREM You need this because you are setting variables (and reading them)REM inside a parenthetical structure (a loop in this case)setlocal enabledelayedexpansionREM The /f switch tells FOR to treat the output of the commandREM as a series of text lines to be processedREM I have changed the variable to upper case to makeREM the variable modification clearerfor /f "delims=" %%A in ('dir /s /b *_MG_*.JPG') do ( REM If %%A is a file path and name (or just a filename) REM %%~dpA is its drive and path... set filepath=%%~dpA REM ... and %%~nxA is its name and extension REM Thus we have removed the drive and path... set filenameold=%%~nxA REM Modify just the name without path REM to get the new name set filenamenew=!filenameold:_MG_=! REM Give the Rename command the full path & old name REM for the file to be renamed, and just the new name rename "!filepath!!filenameold!" "!filenamenew!")
Since we have now got up to 7 pages, I feel justified in having a moan about how standards are slipping here on CH. Somebody posts a question, somebody else (who shall be nameless) posts a partial solution, the OP tries it out, it doesn't work, and then a whole bunch of people just post guesses. Nobody read the command documentation; nobody tried specimen code.