Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a file which contains values like
10AB1CD1278A
10AB5CD12578HJI
Using the below
set var=10AB2CDEFG12345
set "var=%var:~2,4%"
echo %var%
Expected answer "AB1C".Which is 4 characters after the digit 10.
Since the values within the file are of varied length the above doesnt work for all the values contained.
OK, after looking at you comments I think we can understand that you want to create a batch file which goes through every line in a text file and present to you the desired series of four letters. If so, very easy. Run this in the same directory, with the file saved as input.txt
Code :
setlocal enabledelayedexpansion
for /f %%a in (input.txt) do (
set var=%%a
set "var=!var:~2,4!"
echo !var!
)
Note: I kept the commands as they were incase you intended to do something else with it.
Hope this helped, Mona
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I'm trying to create a directory in windows using CMD but the directory names need to be dynamic.
I have a batch script (.bat file) that runs database dumps into a given folder everyday, for instance C:\Users\name\Documents\dump-destination which then contains a bunch of .sql files. Now I need to move all those files into a directory that corresponds to the day's dump date, for instance db-31-12-2020
How can I dynamically create the db-31-12-2020 directory mentioned above (with the day's date) so I can use it below?
move C:\Users\name\Documents\dump-destination\* D:\new-dump-destination\?
This assumes your local date format is dd/mm/yyyy. If it is mm/dd/yyyy, you'll have to swap bits round.
set destdir=db-%date:~0,2%-%date:~3,2%-%date:~6,4%
md D:\new-dump-destination\%destdir%
move C:\Users\name\Documents\dump-destination\* D:\new-dump-destination\%destdir%\
You could use the db-%date:~0,2%-%date:~3,2%-%date:~6,4% string twice instead of using the destdir variable, but if you ran it close to midnight and the clock rolls over between the two calls, it breaks.
Use this code:
#Echo off
:: Save the day, month and year in variables
For /F "Tokens=2,3,4 Delims= \" %%a in ('Date /T') Do Set day=%%a & Set month=%%b & Set year=%%c
:: Create directory
MkDir "D:\new-dump-destination\db-%day%-%month%-%year%"
:: Move files
move "C:\Users\name\Documents\dump-destination\*" "D:\new-dump-destination\db-%day%-%month%-%year%\"
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to run a batch file with the following code:
set dirA=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\In
set dirE=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\OutPut
set dirC=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\Processed
The batch file does not run and shuts down immediately and I cannot figure out what is wrong.
Couple of things you need to understand. When we set variables, we typically enclose them in double quotes in case there are whitespace, which after the whitespace, the next field is seen by cmd as a new command and you will get some errors you would not want, such as the infamous "Is not recognised as an internal command on batch file` error, so we would therefore rather do this:
set "dirA=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\In"
set "dirE=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\OutPut"
set "dirC=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\Processed"
The code then does exactly what you told it to do and that is to simply set variables %dirA% %dirE% and %dirC% with the respected values.
Now to see a result, you need to do something with those variables, perhaps we echo them?
#echo off
set "dirA=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\In"
set "dirE=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\OutPut"
set "dirC=C:\SE_BulkUnzipTarGzipFiles\WinRarSamples\Processed"
echo %dirA%
echo %dirE%
echo %dirC%
pause
Your script runs, but it finishes almost immediately.
If you add a pause command at the end, the window will stay open until you press a key.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to remove ':' from file/directory path d:\apps
Looking for output: d\apps
Cant get away with string substitution. Any help!
#ECHO OFF
SETLOCAL
SET "var=d:\apps"
SET "var2=%var::=%"
SET var
GOTO :EOF
Works perfectly happily for me - what's your actual problem?
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have text file like below strings in multiple rows
1234
234545
123
16322
I need a output like using windows batch script
1234,234545,123,16322
Try this:
#echo off
setlocal EnableDelayedExpansion
set "txt="
set input=input.txt
for /f "delims=" %%a in (%input%) do (
set "txt=!txt!%%a,"
)
set "txt=!txt:~0,-1!"
>new.txt echo !txt!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
I am having an Input file in the location C:/temp/sample.txt, which consists of one word in each line of the text file. For supporting UI, I need each line to be displayed as text file in front end. So I want a batch script which will produce output as text files for each line of the input text file in the same folder. Can you please suggest whether it is possible to make it?
It is possible with the "for" command:
for /f %i in (input.txt) do echo. > %i
Use %%i in batch files instead of %i.
for /f %%i in (c:\temp\sample.txt) do echo %%i > c:\temp\%%i.txt