Batch file doesn't run correctly within Windows 10 [duplicate] - batch-file

This question already has answers here:
Works in Command Prompt but not in BAT-file or CMD-file [duplicate]
(2 answers)
Closed 2 years ago.
I have a merge.bat file which includes the following line:
for %f in (*.txt) do type "%f" >> mergedfile.txt
This code line works perfectly if I enter it directly into a cmd window.
All the txt files are located in the same folder as this merge.bat file.
However, when I try to run it as a batch file under cmd i.e. call merge.bat it shows the following message:
f" >> mergedfile.txt was unexpected at this time
What I would like to do is setup the merge.bat file to run in the Windows 10 task scheduler at a set time, but I just can't get the .bat file to run correctly.
What am I doing wrong?

you have to add double % signs in batch file in loops.use the following code:
for %%f in (*.txt) do type "%%f" >> mergedfile.txt

Related

how do i get echo to type out a % in a batch file [duplicate]

This question already has answers here:
BATCH- echo % (print an percent sign) [duplicate]
(1 answer)
Escape percent in bat file
(1 answer)
Ignore percent sign in batch file
(6 answers)
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
(8 answers)
What is the difference between % and %% in a cmd file?
(2 answers)
Closed 6 days ago.
I want the code below to work or something that does the same thing.
#echo off
echo 100%
pause
When you open the .bat file it shuld look like this.
100%
Press any key to continue . . .
So i want the echo to read out the % and not to be a part of a code.
I want it to work the same as for example
#echo off
echo hi
pause
I have only tried echo 100% because i am a noob at coding so i dont know what else to try.
The percent sign has special meaning in (Windows) batch files, it is used to indicate a variable. For instance, echo %username% will print the contents of the username variable.
To echo a literal % sign, you need to double it, i.e. echo 100%%

Is there any way to access .cmd file path in command prompt? [duplicate]

This question already has answers here:
What does %~dp0 mean, and how does it work?
(7 answers)
What is the current directory in a batch file?
(9 answers)
What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory?
(6 answers)
Closed 8 months ago.
I have a .cmd a file and I want to write some code in it. But first of all I want a code to open the .cmd file path and when someone clicks my .cmd file, it's dir opens in command prompt. Can anyone help me with this?
Thanks
What's mean 'dir opens in command prompt'? If you want to open new cmd window from current dir, just add to your file the following command:
start cmd
Or even
start cmd #cmd /k dir
if you want to see a content of the current directory in that window. Also you can open the current folder in Windows Explorer - just add to your file this command:
explorer .
fixed
Just copy this code in your .cmd file:
cd /d %~dp0
call cmd

using windows batch script to automate unzip task [duplicate]

This question already has an answer here:
Automating task for unzing a gz compressed file using windows batch script [closed]
(1 answer)
Closed 2 years ago.
I have two issues with this code I have attached below,
it doesn't delete the original file after unzip, I want the script to delete the original file from source folder
the converted file is not saved in target folder as set but it creates a tree of the directory in the same folder of the script and saved output there.
Please help me solve this issue
Sample code I have tried
#echo off
set "source=%userprofile%\Desktop\basanta\Automation\a"
set "target=%userprofile%\Desktop\basanta\Automation\b"
FOR %%A IN ("%source%\*.gz") DO (
"%programfiles%\7-zip\7z.exe" x "%%~A" -o"%target%\%%~pA"
del "%%~A" /Y
)
Please help me to write the script as described above
try: del file /f /q
try to enclose -o parameter with a quotes: "-o%target%"

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

Works in Command Prompt but not in BAT-file or CMD-file [duplicate]

This question already has answers here:
Why does FOR loop command not work in a batch file which works on Windows command prompt?
(3 answers)
Closed 4 years ago.
Due to problem with WINDOWS DEFENDER I need do the following:
for /r "%appdata%\..\Local\MyProg\2.0" %D IN ("*MyProgram.exe") DO "%~fD
It works perfect in COMMAND PROMPT but not in batch-file or cmd-file - why?
How do I make in a excutable file as a Batch-file or ".cmd"?
When running for loops in a batch file, you need to use an additional % in predefined variables. So it should be:
for /r "%appdata%\..\Local\MyProg\2.0" %%D IN ("*MyProgram.exe") DO "%%~fD
I suggest you read up using the well documented help by running for /? from cmdline. You will benefit from it, guaranteed!
A recursive For loop already returns the full path, additionally there's already a system variable for the path %AppData%\..\Local.
For /R "%LocalAppData%\MyProg\2.0" %%A In ("*MyProgram.exe") Do "%%A"
Depending upon your needs, it may be worth checking out the Start command usage too, Start /?. You may find that Do Start "" "%%A" is what you needed.

Resources