Saved Bookmarks
| 1. |
Solve : Remove space from file name? |
|
Answer» C:\>type nospace.bat Code: [Select]echo off echo type oldname.txt type oldname.txt sed 's/ //g' oldname.txt > newname.txt echo type newname.txt type newname.txt for /f "delims=" %%i in (oldname.txt) do ( echo %%i call nname.bat %%i ) C:\>type nname.bat Code: [Select]echo off rem Usage: nname.bat * echo inside nname.bat echo %* for /f "delims=" %%i in (newname.txt) do ( echo inside for %%i pause echo copy "c:\%*" %%i copy "%*" %%i pause echo another oldname exit /b ) C:\> Output: C:\>nospace.bat type oldname.txt good name.txt File 2 TEST .txt File 3 test .txt Remove space from file name.txt another file with spaces.txt file with spaces.txt type newname.txt goodname.txt File2test.txt File3test.txt Removespacefromfilename.txt anotherfilewithspaces.txt filewithspaces.txt good name.txt inside nname.bat good name.txt inside for goodname.txt Press any key to continue . . . copy "c:\good name.txt" goodname.txt 1 file(s) copied. Press any key to continue . . . another oldname File 2 test .txt inside nname.bat File 2 test .txt inside for goodname.txt Press any key to continue . . . copy "c:\File 2 test .txt" goodname.txt 1 file(s) copied. Press any key to continue . . . another oldname File 3 test .txt inside nname.bat File 3 test .txt inside for goodname.txt Press any key to continue . . . copy "c:\File 3 test .txt" goodname.txt 1 file(s) copied. Press any key to continue . . . another oldname Remove space from file name.txt inside nname.bat Remove space from file name.txt inside for goodname.txt Press any key to continue . . . copy "c:\Remove space from file name.txt" goodname.txt 1 file(s) copied. Press any key to continue . . . another oldname another file with spaces.txt inside nname.bat another file with spaces.txt inside for goodname.txt Press any key to continue . . . copy "c:\another file with spaces.txt" goodname.txt 1 file(s) copied. Press any key to continue . . . another oldname file with spaces.txt inside nname.bat file with spaces.txt inside for goodname.txt Press any key to continue . . . copy "c:\file with spaces.txt" goodname.txt 1 file(s) copied. Press any key to continue . . . another oldname C:\>Dearest Bill/Greg/Victor/Victoria/Sybil/Dame Edna or whomever you choose to be today. Please stop re-posting my code after you have changed or disassembled it. If I had wanted to use copy I would have. Code: [Select]echo off pushd %cd% cd /d c:\temp setlocal enabledelayedexpansion for /f "tokens=*" %%a in ('dir /a-d /b') do ( set inFile=%%a set newName=!inFile: =! ren "%%a" !newName! ) popd Considering all your experience, I'd have thought you would test the code in a trash directory before running it against live FILES. Showing output should be done with caution. It's doubtful any users have the same file names as you and when they don't get the same results may wonder what they did wrong. In the original post, it should have been made clear that sed is not a part of Windows and a link should have been provided for the download. It should also have pointed out that the way the batch file is written that sed needs to be in a directory on the path for the code to work. I'm just saying... Quote from: Sidewinder on March 21, 2010, 12:48:06 PM "Dear Greg, Showing the output of your excellent code is added value for new users. It is much easier to follow the flow of the code when the output is provided. I will continue to post the output of any code I please. Don't allow the troublemakers through email distort my intentions or your intentions. Keep posting. You are the best code man here. I suspect you are still working in the real world. Quote from: greg on March 21, 2010, 01:35:20 PM Showing the output of your excellent code is added value for new users. When you say "here is the output from sw's code" make sure it is EXACTLY the same as the code he gave, otherwise, it's a of sw's code. that is- state what you changed. Heck, maybe one of these days you might want to actually describe how the batch files work, since, as much as you'd like to say otherwise, a new user to batch doesn't instantly understand how your output translates to function, especially since output is, by definition, full of data specific to your test data. Quote I suspect you are still working in the real world. Evidently, by the fact that you so strongly insist on "output" as some sort of measurement metric of code quality/correctness, you aren't- wether this is due to retirement, or other factors is redundant. While it's true that showing output is certainly one of the many things one can do to display how a piece of code works, it is far from the only thing you should do. pasting a chunk of output from a cmd session LACKS one thing: narration. In order to LEARN from the output, the person needs to understand what is happening. the output from cmd doesn't always reflect that, and due to various manglings that occur when the batch is run in with echo on (a reasonable setting when testing/debugging batch files), for example, double percent signs collapse to single percent signs, various other changed regarding delayed expansion (if used) etc. additionally- the output from a single session only determines the correctness of code in one instance. If I had a batch file: Code: [Select]echo off echo 2 and claimed it did math, by your logic I could simply show the output! Code: [Select]C:\>test.bat 1+1 2 In other words- it proves correctness but only for a single set of input data; and since the input data(various files and folders, in the case of the batch solutions provided here) Is not necessarily the same as what you may have on your machine, it's not usually a very effective one. Pre-emptive snarky comment "where is BC's code?" or some variant thereof: VBScript Code: [Select]dim fileread fileread = WScript.Arguments(0) set FSO = CreateObject("Scripting.FileSystemObject") set ffile = FSO.GetFile(fileread) set tstream = ffile.OpenAsTextStream(1,0) strread = tstream.ReadAll() strread = Replace(strread," ","") tstream.close() set ffile = nothing set tstream = FSO.CreateTextFile(fileread,True,False) tstream.Write strread tstream.close Quote troublemakers through email And the aliens from Neptune causing trouble with their thought beams that direct you to POST THE OUTPUT... Quote from: Salmon Trout on March 21, 2010, 02:08:37 PM And the aliens from Neptune causing trouble with their thought beams that direct you to POST THE OUTPUT... "Beam me up, Scotty!"Now may I please have a turn? The original poster showed this a small batch file with the null spaces and it looked like an invocation of the UNIX said program to remove all spaces from a text file and replacing with only nothing. There was no explanation as to quietness was necessary or wet application this might have. Nor did he provides the contents of the input file. So we had to guess as to what the objective was. We were not told why the spaces had to be removed and replaced with just a null character. A more common and practical thing would be to replace spaces with_is in files that are going to be placed on a Web server that is running an older version of UNIX. Without that information, we all are just shooting in the dark. In a Windows system there is no good reason to remove spaces from a final. It's almost impossible to understand what somebody in state saying if you take out all the spaces. But if you need to do that, it's quite easy to do that a notepad. You can tell notepad to find all spaces and replaced it with dolls. Makes a very interesting document. Now for demonstration of this the following is a short piece that I have edited with notepad and taken out all the spaces but have left in punctuation showed this capitalization and periods. Doeseverybodyhererememberthetelevisionc haractercalledSevinofNine?Shewasanandroidthatlookedveryveryhuman. Ifshestillontelevision?Haven'tseenherforawhile.HerearelocalComcastca blewedon'tgetallthechannelswereusedto,butwecanaffordtopaythe highercostsomaybeweremissingoutonsomeof thenewstaffortheoldstuffthat'soutthereintelevisionwhen.Endoftest. Quote from: Geek-9pm on March 21, 2010, 03:10:15 PM Now may I please have a turn? The topic is about spaces in filename not the contents of the file. Quote from: greg on March 21, 2010, 05:31:48 PM The topic is about spaces in filename not the contents of the file. your Original Post's batch code does otherwise. Quote from: greg on March 21, 2010, 05:31:48 PM The topic is about spaces in filename not the contents of the file.see reply #7 Quote from: ghostdog74 on March 21, 2010, 06:52:41 PM see reply #7 The file used in post one contains a list of old file names with spaces. The files names listed in the file were changed with sed. The spaces were removed from the list of filenames. Later the old files were copied to the new filename. That is not related to Geek removing all the spaces in a text document. Quote from: ghostdog74 on March 20, 2010, 10:28:40 PM subtle differences between your post title and what you are actually doing in your batch. you are removing spaces from the contents of the file, not the file name. The file contained file names. The spaces were removed from the filenames inside the file. Later the oldfiles were copied to a file with a file name without spaces. QED Quote from: greg on March 21, 2010, 07:46:42 PM The file contained file names. The spaces were removed from the filenames inside the file. Later the oldfiles were copied to a file with a file name without spaces. QED? no, your first post did not demonstrate the renaming of files, "physically". your first post only shows us removing spaces from the contents of your file which are filenames (and yes i KNOW what you are trying to do) and saving them to another file. But how to do you rename them using the output of sed? a little psuedocode to demo what i mean Code: [Select]for each (dir /a-d /b) do ( newname = echo filename | sed 's/ //g' <----this is where you remove the spaces.... ren originalfile newname ) Quote from: Geek-9pm on March 21, 2010, 03:10:15 PM DoeseverybodyhererememberthetelevisioncharactercalledSevinofNine?Shewasanandroidthatlookedveryveryhuman. Ifshestillontelevision?Haven'tseenherforawhile. Oh, yeah! Seven of nine was actually a human that was turned into a partial robot, and then mostly turned back into a human. She's currently on the show Leverage on TNT.Jeri Ryan... |
|