Put a command in a variable [duplicate] - batch-file

This question already has answers here:
Assign output of a program to a variable using a MS batch file
(12 answers)
Closed 7 years ago.
I want to store this command in a variable called code1
dir /a:d /b
so the output would be:
Folder1
Folder2
Folder3
etc...
Any ideas how to do it?
I've checked other questions and when it just sais echo is off.

Simply store the command in the variable like you say. To execute, simply expand the value:
#echo off
setlocal
:: Define a simple "macro"
set "code1=dir /a:d /b"
:: Execute the "macro"
%code1%

Related

what is windows equivalent of Touch command to modify timestamp of files in some directory, how to achieve by windows batch built-in commands [duplicate]

This question already has answers here:
Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)
(3 answers)
Closed 2 years ago.
I want to develop batch having step that it will update timestamps of all files within some directory.
Below command gives list of all files in directories.
Can we redirect output of this command to some other windows equivalent touch command, so as to update the timestamps for all files?
cmd>dir /B
Log_adminUser_1.log
Log_adminUser_2.log
Log_adminUser_3.log
Log_adminUser_4.log
Log_adminUser_5.log
Log_adminUser_6.log
As a trick for touching all files in a directory, you could use this:
#ECHO OFF
FOR /F "delims=" %%G IN ('DIR /B') DO (
COPY /B "%%G"+,, "%%G" > NUL
)
The COPY /B construct is documented in TOUCH on SS64, which also explains some caveats with it.

FOR command not executing in script [duplicate]

This question already has answers here:
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 3 years ago.
I receive files via FTP that are placed in a folder called Landing. These files arrive in a folder that is randomly named, for example 000174, and the files inside are named Activity.txt.174 for example.
I have created a small script that allows me to reduce the file name to Activity.txt which works perfectly fine when executed within a command shell but when saved as a batch file it will not execute.
The script line is:
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
I have attempted to add various triggers from other scripts to get this working but when I get this working via CMD it still will not execute from a batch file.
Can anyone help please.
Mike
Change
FOR /R %f IN (*.txt.*) DO REN "%f" *.&
to
FOR /R %%f IN (*.txt.*) DO REN "%%f" *.&
https://ss64.com/nt/for_f.html
Use double percent sign before your variable name.
Batch script
#echo off
FOR /R HERE_YOUR_ROOT_PATH %%f IN (*.txt.*) DO REN "%%f" *.&
#echo on

Batch Script for Check and Cleanup [duplicate]

This question already has answers here:
Check if any type of files exist in a directory using BATCH script
(4 answers)
Closed 5 years ago.
i want to write a batch script where i need to check if a directory is empty or not, if not empty i want to clean that particular directory. Path will passed via command line argument.
Here's some example code to get you started:
#Echo Off
If "%~1"=="" GoTo :EOF
If Not Exist "%~1\" GoTo :EOF
Dir/B/A "%~1\*"|Find /V "">Nul&&Call :CleanIt
Pause
GoTo :EOF
:CleanIt
Rem Your clean command(s) here
You should only need to add your specific code to the bottom

How to make a batch file put every file name in a folder in a different variable with for loop [duplicate]

This question already has answers here:
DIR output into BAT array?
(2 answers)
Closed 7 years ago.
There is a folder called 'weapons', and in this folder are empty files, like 'P250.wep, AWP.wep'. I need to make a batch file that loops through the folder, takes every file name, and puts it in a different variable. For example, after looping these files, P250.wep should be stored in variable weapon1, and AWP.wep in weapon2. Please help me!!!
You can try this method, but it's automatically sort alphabetically before the loop starts. So, this mean AWP.wep will be weapon1, and P250.wep will be weapon2. One more thing, I don't know which one do you want to store, you mentioned "take every file name", but you also mentioned file name with extension... If you don't mind, take a look at the script:
#echo off
Setlocal EnableDelayedExpansion
set count=1
for %%a in (c:\PATH\weapons) do (
set "weapon!count!=%%~na"
set /a count+=1
)
pause >nul

Batch checking if a file exists or not [duplicate]

This question already has answers here:
How to verify if a file exists in a batch file?
(3 answers)
Closed 8 years ago.
I need a batch code that will tell me if test.txt is there or not it is in the same directory as the batch file. I would like to know what I should put in the space where it says the code. Can anyone help?
rem the code
echo yes
rem the code
echo no
IF EXIST yourfilename (
echo Yes
) ELSE (
echo No
)
Note : ELSE command must be on the same line as the end of the IF command.
To explore more type IF /? in cmd.

Resources