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 10130 times)

0 Members and 1 Guest 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