none
Batch file to remove spaces from txt file RRS feed

  • General discussion

  • Hi Gurus,

    I need to create a file bat so that it snippes the contents of the file, I mean that in my file there are columns with data:

    'Jan' '1234' 'Prod' 'John'
    'Feb' '122222' 'Dev' 'Anna'
    and so on

    I would like the whip to make spaces

    'Jan''1234''Prod''John'
    'Feb''122222''Dev''Anna'

    Regards





    • Edited by lukaszEPM Sunday, May 20, 2018 10:58 AM
    • Changed type Bill_Stewart Tuesday, July 31, 2018 3:09 PM
    • Moved by Bill_Stewart Tuesday, July 31, 2018 3:09 PM This is not "scripts on demand"
    Sunday, May 20, 2018 10:54 AM

All replies

  • You should not use cmd/batch anymore. Use Powershell instead. It's more feature rich and way easier to learn.

    But before you might read this first please: This forum is for scripting questions rather than script requests.


    Also find scripts here: PowerShell Gallery or here: TechNet Gallery - resources for IT professionals.

    Learn PowerShell: Microsoft Virtual Academy - Getting Started with Microsoft PowerShell.

    Script requests: Microsoft Technet Script Center - Requests.


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Sunday, May 20, 2018 11:31 AM
  • Try this:

    $data = "'Jan' '1234' 'Prod' 'John'
    'Feb' '122222' 'Dev' 'Anna'"
    
    $data -replace ' ',''

    Ans please have a look at the links posted by @jrv and @BOfH_666 to learn PowerShell the right way.

    Tuesday, May 22, 2018 7:02 AM
  • Not intending to denigrate the previous posters, but there might be significant reasons to do this in batch.  An example is the steep learning curve of PowerShell coupled with a need to turn something around right now. Another is the need to activate PowerShell, which might be prohibited by a group policy in an enterprise environment.

    If learning time or security policies are not the issue, then, by all means, learn PowerShell.  It's much more powerful than anything that has come before.  However, in the interim, I would offer that with enough knowledge of batch, this particular problem requires a very modest amount of effort to construct a reasonable batch solution, something like this:

     @echo off
      if "%1"=="" echo No input file. Nothing done.&exit /b 1
      if not exist %1 echo File %1 not found. Nothing done.&exit /b 2
      if "%2"=="" %0 %1 %~dp1Outfile.txt
      (for /f "tokens=1-8" %%A in (%1) do @echo.%%A%%B%%C%%D%%E%%F%%G%%H)>%2

    It has two command line arguments; the input file pathspec and the output file pathspec.  If a file is dropped onto the batch procedure file, via the File Explorer or a shortcut on the desktop, a default filename, Output.txt,  is provided and is stored in the file folder containing the input file.

    For future reference, find information on the particular construction of the FOR statement used here at FOR /? from the command line.

    PS. Not being a group policy guru, nor proficient in PowerShell, I offer this information humbly without any intention of saying this is a better way or to start a flame war with the better equipped members of this forum.


    Tom Lavedas

    Tuesday, June 26, 2018 7:18 PM
  • Not intending to denigrate the previous posters, but there might be significant reasons to do this in batch. An example is the steep learning curve of PowerShell coupled with a need to turn something around right now.
    I would suggest that cmd.exe batch has a higher learning curve than PowerShell due to its numerous arcane syntax quirks.
    Another is the need to activate PowerShell, which might be prohibited by a group policy in an enterprise environment.

    This may be a misunderstanding. If you are referring to the PowerShell execution policy: Execution policy is an administrator safety feature, not a security boundary. If an administrator sets a restrictive execution policy in the mistaken notion that it somehow increases security, it is easily bypassed by specifying the -ExecutionPolicy parameter on the powershell.exe command line.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 26, 2018 8:51 PM
  • Bill,

    Yes, the syntax is complicated, but my assumption was that since batch has been around since the dark ages of computing the learning curve might have been climbed already.  Probably a bit of an overly optimistic assumption.  Plus, I purposely tried to forestall a lot of discussion on the subject by trying to caveat my response to say it was only of limited application in some specific instances.

    Also, as I said, I'm not a security expert.  I just know that the few times I've venture into PowerShell, getting it to run has always seemed like a hassle to me.  A result of my lack of understanding, I suppose.

    Now, since retiring from the daily grind, I find it hard to imagine climbing the curve.  Just no incentive (and the 'old dog' syndrome).


    Tom Lavedas

    Wednesday, June 27, 2018 2:29 AM
  • Bill,

    Yes, the syntax is complicated, but my assumption was that since batch has been around since the dark ages of computing the learning curve might have been climbed already.  Probably a bit of an overly optimistic assumption.  Plus, I purposely tried to forestall a lot of discussion on the subject by trying to caveat my response to say it was only of limited application in some specific instances.

    Also, as I said, I'm not a security expert.  I just know that the few times I've venture into PowerShell, getting it to run has always seemed like a hassle to me.  A result of my lack of understanding, I suppose.

    Now, since retiring from the daily grind, I find it hard to imagine climbing the curve.  Just no incentive (and the 'old dog' syndrome).


    Tom Lavedas

    Tom,

    Take care of those old bones but don't let the bones between your ears get brittle too.

    Here is a good tutorial that will excite you and help you see the error in your ways.  PS is much easier to learn than batch or VBScript and also more fun.


    \_(ツ)_/

    Wednesday, June 27, 2018 2:34 AM
  • Thanks jrv.  That was interesting.  I've opened the PowerShell console.

    Tom Lavedas

    Wednesday, June 27, 2018 2:14 PM
  • Wednesday, June 27, 2018 2:17 PM
  • Thanks jrv.  That was interesting.  I've opened the PowerShell console.

    Tom Lavedas

    Hey.  I am having fun with PosH and I am definitely older and creakier than you.


    \_(ツ)_/

    Wednesday, June 27, 2018 2:42 PM