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

0 Members and 1 Guest are viewing this topic.

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. "