Windows CMD create dynamically named directory [closed] - windows

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%\"

Related

What does percent sign with tilde syntax(%~a) mean in command line [closed]

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 5 years ago.
Improve this question
for %a in (*.txt) do ren "%~a" "%~na version 1%~xa"
This command line script can rename files in a directory by adding text ( version 1) to the end of filenames.
I'm wondering what do %a, %~a, %~na, %~xa syntax mean? Where can I find an introduction to these online?
Could you give me an online link to documentation?
Try checking the documentation for the for command:
https://technet.microsoft.com/en-us/library/bb490909.aspx
%~a means expand without quotes, %~na means filename only, %~xa means file extension only.
So "%~a" is the full name with quotes always around it, then the target is the existing name with "version 1" inserted between the name and the extension.

windows batch script to combine multiple rows to a comma separated single row [closed]

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!

Use substring against a file with varied length strings [closed]

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

Batch script to read input text file and make text file for each line of input text file [closed]

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

Looking for windows macro type software [closed]

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 8 years ago.
Improve this question
Im looking for something that will allow me to perform simple batch-type automation in the Win XP operating system.
I need to, for example, pre-pend the name of every folder a file is nested in to it's filename.
So, before: blah\yep\dave\robert.txt
after: blah\yep\dave\blah_yep_dave_robert.txt
I would also like to change all the files to read-only mode.
I used to have a tool that did this but I forgot its name.
This can be done with a batch file:
#echo off
setlocal enableextensions enabledelayedexpansion
set PathSegmentToIgnore=%~dp0
for /r %%F in (*) do (
if not exist %%F\NUL if not "%%~F"=="%~dpnx0" (
rem ignore folders
set "FilePath=%%~dpF"
set "FilePath=!FilePath:%PathSegmentToIgnore%=!"
set "FilePath=!FilePath!%%~nxF"
set "FilePath=!FilePath:\=_!"
echo Renaming "%%~nxF" to "!FilePath!"
move "%%~F" "%%~dpF!FilePath!" >nul 2>&1
)
)
Use VBScript or Powershell. Both are good options for scripting.

Resources