Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Remove space from file name  (Read 10131 times)

0 Members and 2 Guests are viewing this topic.

greg

    Topic Starter


    Intermediate

    Thanked: 7
    Remove space from file name
    « on: March 20, 2010, 03:23:36 PM »

    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
    )

    Output:

    C:\>nospace.bat

    type oldname.txt
    good name
    File 2 test .txt
    File 3 test .txt
    type newname.txt
    goodname
    File2test.txt
    File3test.txt

    C:\>
    Have a Nice Day

    Salmon Trout

    • Guest
    Re: Remove space from file name
    « Reply #1 on: March 20, 2010, 05:31:15 PM »
    Wow, greg, you're a genius!

    I am pleased to award you a...


    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Remove space from file name
    « Reply #2 on: March 20, 2010, 07:24:25 PM »
    Quote
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    D:\BATCH>nospace
    type oldname.txt
    The system cannot find the file specified.
    'sed' is not recognized as an internal or external command,
    operable program or batch file.
    type newname.txt
    D:\BATCH>

    Does not work as advertised!

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Remove space from file name
    « Reply #3 on: March 20, 2010, 07:24:46 PM »
    Does not work as advertised!

    heh, I was just going to post that...
    I was trying to dereference Null Pointers before it was cool.

    greg

      Topic Starter


      Intermediate

      Thanked: 7
      Re: Remove space from file name
      « Reply #4 on: March 20, 2010, 07:52:16 PM »
      Does not work as advertised!

      Thanks for the correction. 
      What will work? 
      What will  remove the spaces from a file name?

      What is correct batch command?

      I appreciate the help offered by Geek.

      Have a Nice Day

      greg

        Topic Starter


        Intermediate

        Thanked: 7
        Re: Remove space from file name
        « Reply #5 on: March 20, 2010, 08:27:07 PM »
        Does not work as advertised!


        Modifying/Removing a File
        Use the following procedure to rename and then modify a file with a space in its name:


        Use the Batch RENAME command with a wildcard to replace the space with a legal filename character. For example:
             
        rename test?car.txt test_car.txt
                          

        This example replaces the space with the underscore character.

        http://support.microsoft.com/kb/65163

        Thanks, for the help Geek. There is always only one correct solution.

        Have a Nice Day

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: Remove space from file name
        « Reply #6 on: March 20, 2010, 08:33:54 PM »
        Quote
        Thanks, for the help Geek. There is always only one correct solution.

        I disagree...
        There are always many solutions.
        " Anyone who goes to a psychiatrist should have his head examined. "

        ghostdog74



          Specialist

          Thanked: 27
          Re: Remove space from file name
          « Reply #7 on: March 20, 2010, 10:28:40 PM »
          What will  remove the spaces from a file name?
          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. 

          Salmon Trout

          • Guest
          Re: Remove space from file name
          « Reply #8 on: March 21, 2010, 02:11:59 AM »
          There is always only one correct solution.

          What a very stupid remark.

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: Remove space from file name
          « Reply #9 on: March 21, 2010, 04:13:08 AM »
          Quote
          Use the Batch RENAME command with a wildcard to replace the space with a legal filename character.

          Using wildcards in a rename operation can have unintended consequences. There is also the condition where there is more than one space in a file name, not always in the same position.

          Not sure why sed is being used. It's a download on XP machines and this can be done with native tools.

          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

          Feel free to change the directory name. 8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          ghostdog74



            Specialist

            Thanked: 27
            Re: Remove space from file name
            « Reply #10 on: March 21, 2010, 05:28:01 AM »
            Not sure why sed is being used. It's a download on XP machines and this can be done with native tools.
            its just a tool to do the job and do it well indeed. having it in your toolbox is no harm, i don't know why downloading stuff is still frowned upon these days. Its just a one time download anyway. (and its just one executable which you can bring anywhere, same as any other resource kit tools from MS).

            Code: [Select]
            C:\test>dir /b /a-d *file*
            another file with spaces.txt
            file   with    spaces.txt

            C:\test>dir /b /a-d | sed  -n "/ /{h;s/ //g;x;s/.*/mv '&' /;G;s/\n//;p}"
            mv 'another file with spaces.txt' anotherfilewithspaces.txt
            mv 'file   with    spaces.txt' filewithspaces.txt

            C:\test>dir /b /a-d | sed  -n "/ /{h;s/ //g;x;s/.*/mv '&' /;G;s/\n//;p}" |cmd 

            you could guess the above doesn't really work (we are dealing with cmd.exe after all, not *nix shell :) 
            « Last Edit: March 21, 2010, 06:11:45 AM by ghostdog74 »

            Salmon Trout

            • Guest
            Re: Remove space from file name
            « Reply #11 on: March 21, 2010, 06:23:51 AM »
            i don't know why downloading stuff is still frowned upon these days.

            You do get a number of people on here who appear to be rather un-knowledgeable (or lazy) sysadmins or users in some kind of "corporate" environment who appear to be restricted to using whatever is already installed.

            greg

              Topic Starter


              Intermediate

              Thanked: 7
              Re: Remove space from file name
              « Reply #12 on: March 21, 2010, 10:29:42 AM »
              Feel free to change the directory name.

              Output  for sw post #9


              C:\TEMP>dir
               Volume in drive C has no label.
               Volume Serial Number is F4A3-D6B3

               Directory of C:\TEMP

              03/21/2010  11:21 AM    <DIR>          .
              03/21/2010  11:21 AM    <DIR>          ..
              03/21/2010  11:19 AM                 8 another file with spaces.txt
              03/21/2010  11:18 AM                 8 file   with    spaces.txt
              03/21/2010  11:20 AM                 8 File 2 test .txt
              03/21/2010  11:20 AM                 8 File 3 test .txt
              03/21/2010  11:21 AM                 8 good name.txt
              03/21/2010  11:19 AM                 8 Remove space from file name.txt
                             6 File(s)             48 bytes
                             2 Dir(s)  297,718,951,936 bytes free

              C:\TEMP>cd ..

              C:\>
              Code: [Select]
              type swspaces.bat
              @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: =!
                copy "%%a" !newName!
              )
              popd

              Output:
              C:\>swspaces.bat
                      1 file(s) copied.
                      1 file(s) copied.
                      1 file(s) copied.
                      1 file(s) copied.
                      1 file(s) copied.
                      1 file(s) copied.
              C:\TEMP>dir
               Volume in drive C has no label.
               Volume Serial Number is F4A3-D6B3

               Directory of C:\TEMP

              03/21/2010  11:27 AM    <DIR>          .
              03/21/2010  11:27 AM    <DIR>          ..
              03/21/2010  11:19 AM                 8 another file with spaces.txt
              03/21/2010  11:19 AM                 8 anotherfilewithspaces.txt
              03/21/2010  11:18 AM                 8 file   with    spaces.txt
              03/21/2010  11:20 AM                 8 File 2 test .txt
              03/21/2010  11:20 AM                 8 File 3 test .txt
              03/21/2010  11:20 AM                 8 File2test.txt
              03/21/2010  11:20 AM                 8 File3test.txt
              03/21/2010  11:18 AM                 8 filewithspaces.txt
              03/21/2010  11:21 AM                 8 good name.txt
              03/21/2010  11:21 AM                 8 goodname.txt
              03/21/2010  11:19 AM                 8 Remove space from file name.txt
              03/21/2010  11:19 AM                 8 Removespacefromfilename.txt
                            12 File(s)             96 bytes
                             2 Dir(s)  297,718,951,936 bytes free

              C:\TEMP>
              Have a Nice Day

              Helpmeh



                Guru

              • Roar.
              • Thanked: 123
                • Yes
                • Yes
              • Computer: Specs
              • Experience: Familiar
              • OS: Windows 8
              Re: Remove space from file name
              « Reply #13 on: March 21, 2010, 11:27:13 AM »
              Output  for sw post #9
              AND...?
              You don't need to post the output of other people's codes if it works fine.
              Where's MagicSpeed?
              Quote from: 'matt'
              He's playing a game called IRL. Great graphics, *censored* gameplay.

              greg

                Topic Starter


                Intermediate

                Thanked: 7
                Re: Remove space from file name
                « Reply #14 on: March 21, 2010, 11:34:33 AM »
                You don't need to post the output of other people's code if it works fine.

                It is very good idea to post output. 
                Many readers of these posts do know how to test code.
                Have a Nice Day

                greg

                  Topic Starter


                  Intermediate

                  Thanked: 7
                  Re: Remove space from file name
                  « Reply #15 on: March 21, 2010, 11:39:01 AM »

                  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:\>
                  Have a Nice Day

                  Sidewinder



                    Guru

                    Thanked: 139
                  • Experience: Familiar
                  • OS: Windows 10
                  Re: Remove space from file name
                  « Reply #16 on: March 21, 2010, 12:48:06 PM »
                  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... 8)

                  The true sign of intelligence is not knowledge but imagination.

                  -- Albert Einstein

                  greg

                    Topic Starter


                    Intermediate

                    Thanked: 7
                    Re: Remove space from file name
                    « Reply #17 on: March 21, 2010, 01:35:20 PM »
                    "Dear Greg,
                    Please stop re-posting my code after you have changed or disassembled it. If I had wanted to use copy I would have."

                    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.
                    Have a Nice Day

                    BC_Programmer


                      Mastermind
                    • Typing is no substitute for thinking.
                    • Thanked: 1140
                      • Yes
                      • Yes
                      • BC-Programming.com
                    • Certifications: List
                    • Computer: Specs
                    • Experience: Beginner
                    • OS: Windows 11
                    Re: Remove space from file name
                    « Reply #18 on: March 21, 2010, 01:57:46 PM »
                    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.


                    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 <modified version> 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
                    I was trying to dereference Null Pointers before it was cool.

                    Salmon Trout

                    • Guest
                    Re: Remove space from file name
                    « Reply #19 on: March 21, 2010, 02:08:37 PM »
                    Quote
                    troublemakers through email

                    And the aliens from Neptune causing trouble with their thought beams that direct you to POST THE OUTPUT...


                    greg

                      Topic Starter


                      Intermediate

                      Thanked: 7
                      Re: Remove space from file name
                      « Reply #20 on: March 21, 2010, 02:30:39 PM »
                      And the aliens from Neptune causing trouble with their thought beams that direct you to POST THE OUTPUT...



                      "Beam me up, Scotty!"
                      Have a Nice Day

                      Geek-9pm


                        Mastermind
                      • Geek After Dark
                      • Thanked: 1026
                        • Gekk9pm bnlog
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Expert
                      • OS: Windows 10
                      Re: Remove space from file name
                      « Reply #21 on: March 21, 2010, 03:10:15 PM »
                      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.


                      greg

                        Topic Starter


                        Intermediate

                        Thanked: 7
                        Re: Remove space from file name
                        « Reply #22 on: March 21, 2010, 05:31:48 PM »
                        Now may I please have a turn?

                        The topic is about spaces in filename not the contents of the file.
                        Have a Nice Day

                        BC_Programmer


                          Mastermind
                        • Typing is no substitute for thinking.
                        • Thanked: 1140
                          • Yes
                          • Yes
                          • BC-Programming.com
                        • Certifications: List
                        • Computer: Specs
                        • Experience: Beginner
                        • OS: Windows 11
                        Re: Remove space from file name
                        « Reply #23 on: March 21, 2010, 05:38:12 PM »
                        The topic is about spaces in filename not the contents of the file.

                        your Original Post's batch code does otherwise.
                        I was trying to dereference Null Pointers before it was cool.

                        ghostdog74



                          Specialist

                          Thanked: 27
                          Re: Remove space from file name
                          « Reply #24 on: March 21, 2010, 06:52:41 PM »
                          The topic is about spaces in filename not the contents of the file.
                          see reply #7

                          greg

                            Topic Starter


                            Intermediate

                            Thanked: 7
                            Re: Remove space from file name
                            « Reply #25 on: March 21, 2010, 07:41:37 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.
                            Have a Nice Day

                            greg

                              Topic Starter


                              Intermediate

                              Thanked: 7
                              Re: Remove space from file name
                              « Reply #26 on: March 21, 2010, 07:46:42 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
                              Have a Nice Day

                              ghostdog74



                                Specialist

                                Thanked: 27
                                Re: Remove space from file name
                                « Reply #27 on: March 21, 2010, 08:02: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

                                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
                                )

                                rthompson80819



                                  Specialist

                                  Thanked: 94
                                • Experience: Experienced
                                • OS: Windows 7
                                Re: Remove space from file name
                                « Reply #28 on: March 21, 2010, 08:29:58 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.

                                patio

                                • Moderator


                                • Genius
                                • Maud' Dib
                                • Thanked: 1769
                                  • Yes
                                • Experience: Beginner
                                • OS: Windows 7
                                Re: Remove space from file name
                                « Reply #29 on: March 21, 2010, 08:55:24 PM »
                                Jeri Ryan...
                                " Anyone who goes to a psychiatrist should have his head examined. "