Computer Hope

Microsoft => Microsoft DOS => Topic started by: beasty on July 04, 2011, 07:05:52 AM

Title: Deleting characters from a string through a batch command
Post by: beasty on July 04, 2011, 07:05:52 AM
I'm trying to write a batch command that deletes characters from a file and then outputs the new file into a folder.

The file in Question is below:

bjobojd0000004609142355sd.txt

Everytime the batch file is ran i need it to remove all the characters from the 16th and the 24th position so it reads bjobojd00000046sd instead. If anyone knows how to do this would you be kind enough to let me know how to perform this.

Many Thanks

Beasty
Title: Re: Deleting characters from a string through a batch command
Post by: Sidewinder on July 04, 2011, 08:38:11 AM
This was done by extracting the characters you want to remove and using them as a search argument to be replaced by nulls. The set statement uses zero based offsets, not one based positions.

Code: [Select]
@echo off
setlocal

set oldFile=bjobojd0000004609142355sd.txt
set mask=%oldFile:~15,8%

call set newFile=%%oldFile:%mask%=%%
echo copy %oldFile% %newFile%

The code is based on the file names posted. Fixup any paths as necessary.

Good luck.  8)
Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 04, 2011, 09:27:34 AM
Thanks very much i will give it a go

Beasty
Title: Re: Deleting characters from a string through a batch command
Post by: Salmon Trout on July 04, 2011, 11:04:53 AM
To beasty: did you want to lose the.txt extension? Because that is what your post implies.

Before

Quote from: beasty
bjobojd0000004609142355sd.txt

After

Quote from: beasty
so it reads bjobojd00000046sd instead
Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 05, 2011, 02:33:15 AM
Yes i would like to lose the .txt

One of our systems regularly creates a file of around 26 characters long which is to long for a contractors system to input into there system. So basically what i was asking was everytime a similar file is created, a batch command removes the characters from the 16th and 24th position in the string so it can be accepted into the Contractors system.

I'm not the best with batch files, Sidewinder below has given me something to try.
Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 05, 2011, 03:03:22 AM
The mask instruction is working perfectly but what i have found out today is that there will be multiple files in a directory that will need changing simultaneously and all will have different date and time values in the characters being removed. Any help would be appreciated  :)
Title: Re: Deleting characters from a string through a batch command
Post by: Sidewinder on July 05, 2011, 06:03:07 AM
The for instruction changes the the entire personality of a batch file. A subroutine was used to cut down on the complexity of the code.

Code: [Select]
@echo off
setlocal enabledelayedexpansion

for /f "tokens=* delims=" %%v in ('dir /b *.txt') do (
  set oldfile=%%v
  for /f "tokens=* delims=" %%i in ("!oldFile!") do (
    set newFile=%%~ni
    call :doit
  )

goto :eof

:doit
  set mask=%oldFile:~15,8%
  call set newFile=%%newFile:%mask%=%%
  echo copy %oldFile% %newFile%
 

The last line of the code is prefaced with an echo command. This allows you to see the possible results without actually creating any files. Remove the word echo when satisfied. 

When removing characters from file names be aware you run the risk of creating duplicate file names. Code is set up to run from the same directory as the .txt files are located.

Good luck.  8)

BTW: positions 16-24 is 9 characters, but the example posted only shows 8 character for removal. If the number of removed characters varies, a better solution would be VBScript which has functions for better inspecting the data.
Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 05, 2011, 07:40:09 AM
If you wouldn't mind could you give me a little bit more of an explanation.

Where it says ('dir /b *.txt') i take it the 'dir stands for the directory in which the multiple files are located?

If i gave you an example i.e. There are multiple files in a folder called c:\bobjects\sample jobs and all the files need to have the characters removed. Does that need to go in ('dir /b *.txt').

What about outputting the amended files into a different folder?

Excuse my lack of knowledge in this area as i do not tend to get involved to much with batch scripts and i'm struggling!!!  ???

You have been more than helpful by the way
Title: Re: Deleting characters from a string through a batch command
Post by: Sidewinder on July 05, 2011, 09:18:11 AM
This should fix it up. Added a pushd instruction to set the current folder to where the txt files live, and a popd instruction to navigate the user back to the starting folder. The batch file and the text files do not have to live in the same folder.

Also changed the last line of code so you can see how to direct the output to the folder of your choice.

Code: [Select]
@echo off
setlocal enabledelayedexpansion
pushd c:\bobjects\sample jobs

for /f "tokens=* delims=" %%v in ('dir /b *.txt') do (
  set oldfile=%%v
  for /f "tokens=* delims=" %%i in ("!oldFile!") do (
    set newFile=%%~ni
    call :doit
  )


popd
goto :eof

:doit
  set mask=%oldFile:~15,8%
  call set newFile=%%newFile:%mask%=%%
  echo copy %oldFile% drive:\path\%newFile%

It still holds true that removing characters from otherwise unique file names runs the risk of duplicates and file overwrites. 

Happy coding  8)
Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 05, 2011, 10:30:54 AM
Right i've got a little bit further, the first bit seems to work and it copies the files but it won't copy files into another folder.

echo off

setlocal enabledelayedexpansion

pushd C:\bobjects\SampleJobs


for /f "tokens=* delims=" %%v in ('dir /b *.*') do (
  set oldfile=%%v
  for /f "tokens=* delims=" %%i in ("!oldFile!") do (
    set newFile=%%~ni
    call :doit
  )


pause

popd
goto :eof


:doit
  set mask=%oldFile:~15,8%
  call set newFile=%%newFile:%mask%=%%

echo copy %oldFile% c:\bobjects\test\%newFile%

Have i missed something :)
Title: Re: Deleting characters from a string through a batch command
Post by: Salmon Trout on July 05, 2011, 10:46:41 AM
Right i've got a little bit further, the first bit seems to work and it copies the files but it won't copy files into another folder.

echo copy %oldFile% c:\bobjects\test\%newFile%

Have i missed something :)

Remove the echo?

Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 05, 2011, 11:10:22 AM
Thanks i'm getting somewhere now ;D
Title: Re: Deleting characters from a string through a batch command
Post by: beasty on July 11, 2011, 02:41:43 AM
Hi

I'm still having a couple of problem's but i am getting further.

I need to incorporate a call command with in the script below:

@echo off

rem W-Dixon Test repairs out
rem echo Press Ctrl+C or close the window to stop or
rem pause
echo Working........................

net use D: \\"ipaddress"\test_var /persistent:no

C:
cd \wdftp
rmdir wdtestoutold /s /q
ren wdtestout wdtestoutold
mkdir wdtestout
cd \wdftp\wdtestout

rem Copy test files from orchard/Test_var to wdftp folder for ftp to W-Dixon
xcopy D:\l\test\var\pmcli\ifc\output\wd\* c:\wdftp\wdtestout /v /y
xcopy c:\wdftp\wdtestout\* c:\wdftp\wdtestoutold /v /y
ftp -i -s:"c:\batch\wdtest.ftp" ip address
del /q D:\l\test\var\pmcli\ifc\output\wd\*
del /q c:\wdftp\wdtestout\*

echo .
echo wdtestout finished

rem pause

The call command needs to go just under the second xcopy in the file and needs to call the script earlier in the post using call instead of push and then needs to be out putted to an FTP folder:

pushd C:\wdftp\wdconvert


for /f "tokens=* delims=" %%v in ('dir /b *.*') do (
  set oldfile=%%v
  for /f "tokens=* delims=" %%i in ("!oldFile!") do (
    set newFile=%%~ni
    call :doit
  )


popd
goto :eof
:doit
  set mask=%oldFile:~15,8%
  call set newFile=%%newFile:%mask%=%%
 copy %oldFile% c:\wdftp\wdconvert\%newFile%

Would someone be kind enough to show me how to do this?

Thanks

Beasty

Title: Re: Deleting characters from a string through a batch command
Post by: Sidewinder on July 11, 2011, 11:07:24 AM
beasty,

You do yourself a disservice by requesting stuff in dribs and drabs. Many responders will see your thread has 12 replies and figure they have nothing to add or the problem was solved. Better to split your request into multiple posts or ask all your questions in your first post.

Unless I missed a day in Batch Coding 101, The call statement and the pushd statement are unrelated. The call is used in a batch file to give control to a secondary batch file located somewhere in your system. It sets up a mechanism so when the secondary file terminates, control is passed back to the calling program at the statement following the call which then continues executing.

The pushd statement is like the cd statement with benefits. Pushd saves the current directory on the stack and then changes to the directory passed as the first argument on the command line. The opposite of pushd is popd which navigates to the directory pulled from the stack. The order is last in, first out (LIFO), so it pays to keep track of this info. It's useful on forums where the OP fails to mention path information.

Ex: c:\temp> pushd c:\windows
   this will save the c:\temp directory on the stack and then navigate to the c:\windows directory.

Insert the call statement after the second xcopy as call scriptfromearlierpost.bat If the called file is not in the current directory you will need to supply path information. Same as for the file that ends up in the FTP folder.


 8)
Title: Re: Deleting characters from a string through a batch command
Post by: Salmon Trout on July 11, 2011, 11:31:45 AM
The call is used in a batch file to give control to a secondary batch file located somewhere in your system. It sets up a mechanism so when the secondary file terminates, control is passed back to the calling program at the statement following the call which then continues executing.

In addition to the above, CALL can also call a subroutine within the batch file and as beasty is trying to do, CALL can also be used to run any internal command (SET, ECHO etc) expanding any environment variables passed on the same line.
Title: Re: Deleting characters from a string through a batch command
Post by: markrbts on March 29, 2017, 12:04:15 PM
Hi,

I have read your code and your comments for this post.

I have a group of files I need to remove the last 15 characters form.  The goal is to end up with a pdf extension only.  I am trying to adapt your code to my needs.  I can get this to work if I hard code the file name, but I cannot count on the file names being the same all the time.  The problem seems to be in the for statement.   I am not sure if I need the tokens or the delimiters.

Attached is my adaptation of the code.

[attachment deleted by admin to conserve space]