| 1. |
Solve : Deleting characters from a string through a batch command? |
|
Answer» I'm trying to write a batch command that deletes characters from a file and then outputs the new file into a folder. bjobojd0000004609142355sd.txt After Quote from: beasty so it reads bjobojd00000046sd insteadYes 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. 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 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. 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.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 wayThis 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 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 Quote from: 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. Remove the echo? Thanks i'm getting somewhere now 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 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. Quote from: Sidewinder on July 11, 2011, 11:07:24 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. |
|