Batch Coding for MS-DOS - sorting

I would like to sort and move files with batch coding, is that possible?
Let's say, I have following files lists in folder :
A1.txt
C1.txt
A1.pdf
C1.doc
A1.doc
B1.pdf
B1.txt
C1.pdf
B1.doc
When I run the DOS batch file without parameters and coding file name in batch scripting, The batch file will sort the file and create folder according to file name.
"A1.txt A1.pdf A1.doc" should move in folder called A1 which is automatically, likewise, B1 and C1 folder should be created, the files should be moved in their folder.

If you actually want a Windows batch file,
for %%q in (*) do (
if not exist "%%~nq" md "%%~nq"
move "%%q" "%%~nq"
)
Run "help for" for documentation on "for" and the "~" notation.
(Written for a batch file. If you want to run the command from the prompt, use one "%" instead of two.)

Related

Get the filename of a running batch file

I am trying to make a batch file that will write the name we names it.
For example if i names the file "test1" and start it it will change the title only to "test1" If i change the name to "bat file" it will display "bat file" in the title.
Is it possible to make a file that will write to a .txt file with the same name as the bat file? I am trying to make a batch file that will log sometings and write it too ".txt" where is the name of the bat file without extension
In batch files %0 stores a reference to the current batch file. The exact format of the value depends on how the file is invoked, but there are a serie of modifiers that can be used to retrieve the needed information (You can see the documentation or execute for /?, as the help for this command also includes the full list of modifiers)
#echo off
title %~n0
echo I am %~n0
> "%~dpn0.txt" echo This goes to the file
Where
%~n0 = name of the current batch file (without extension)
%~dpn0 = drive, path and file name (without extension) of the current batch file
echo %~f0 for writing your batch file name. Use ">" to write anything to a file. For eg use echo "write this" > file.txt

Listing the files inside a folder using batch file

I am using a batch file to view the contents (xml files) of a certain folder.
I need to use a batch file for it.
Found this command on internet and it is working perfectly when I type in the command prompt, but when I type it in and save as a batch file, it doesn't give any out put at all. (Basically not running the content)
FOR /R D:\Myfolder %F in (*.*) do rename %~nF.xml %~nf1.xml
There are no restrictions in the folder either.
Shouldn't have worked from the prompt.
In a batch file, you need to change any % for the metavariable (%F in this case) to %%.
Having said that however, (or, from the prompt, %F and %f) are two different animals. It's virtually the only situation where batch is case-sensitive. Your command attempts to rename files using their NAME only (~nF) so if it was to encounter fred.txt it would attempt to rename Fred.xml to -er, 1.xml (I think, maybe %~nf.xml)
Best to say what you're trying to do. We're reasonably slick at crufting up solutions...

Using drag and drop from another directory onto a batch file does not work

I have a folder structure that looks like this:
project
bin
my_program.exe
misc_stuff.exe
DROP_OVER_ME.bat
input_file.txt
Basically, I want to be able to drag and drop the input file on top of the DROP_OVER_ME.bat batch file and have it pass on the path of the input file to the exe.
This is what my batch file looks like:
#echo off
start bin/my_program.exe %1
exit
When I drag and drop input_file.txt over the batch file, everything works fine -- my_program.exe successfully receives the path of the input file and runs.
However, when input_file.txt is located outside the project folder, dragging and dropping it makes the batch file throw up a popup message saying
Windows cannot find 'bin/my_program.exe'. Make sure you typed the name correctly, and then try again.
How can I fix my batch file so I can drag and drop files from any arbitrary location inside my filesystem?
Sounds like the batch script is basing the current working directory as the directory you're dragging from, and not the directory containing the script. (You can test this by adding an echo %cd% && pause to your script if you wish.) Try modifying your script as follows to remove any ambiguity about file paths:
#echo off
cd /d "%~dp0"
start "" "bin\my_program.exe" "%~f1"
exit /b

help with windows batch scripting basics - execution and calling a separate executable within the script

Newbie to windows scripting. I need help running the .bat file on the command line so I can test it.
I used Text Document as my editor to create the file (opens up also as Notepad).
I performed file "save as" (ALL FILES). If I open up cmd, I can see the file has a .txt extension (myfile.bat.txt). So if I just type in cmd myfile.bat.txt the editor opens. I am not sure how to execute this correctly.
As for the logic in my batch script, I am basically logging into a remote directory (already created the net mount) and now I want to:
run an executeable file
rename some files.
With some research, I written this so far. I have saved it as a .bat file
# echo off
echo This is a batch file to run an executable and rename some files
pause
--run executable file here, just don't know how to do it
x:
cd x:
rename fileA fileB
Any help, good tips/practice would be great. Thanks.
Type in this command in cmd window:
rename myfile.bat.txt myfile.bat
Now you can run the script by simply invoking:
myfile.bat
or
myfile
(provided there's no myfile.exe or myfile.com in the same directory).
If you need to edit the script further, you can either right click it in Explorer and choose Edit or call the editor from the command window:
notepad myfile.bat
To call a program from the script, simply add its name, if it's in the current directory:
someprogram.exe
or the name with the path, if it's somewhere else:
directory\program.exe
or
d:\directory\program.exe
If the name or the path contain spaces, be sure to enclose the entire name & path string in double quotes:
"d:\directory\program name.exe"
you can just type the full name of the program
eg
"c:\program dir\program.exe"
or you can add the program directory to your path environment variable
set PATH=%PATH%;"c:\program dir"
and just type the program name
program
you can also edit your PATH variable in windows http://support.microsoft.com/kb/310519
NOTE: When you save the file in notepad, you want to save it as filename.BAT and select All Files from the second dropdown. If you don't it still gets saved as a .TXT.
A couple of command to consider:
CSCRIPT cscript /? in CMD
START http://ss64.com/nt/start.html
If you're doing say a VBSCRIPT use CSCRIPT to start it. If you're trying to execute another BATCH script or an EXE, use START

How to move output file in dos

I would like to create a batch file which will move the output file of a custom command "mdmg C:\source i5". I must execute this command from the C:\home directory where the mdmg.cmd resides.
This command converts the any file in the source dir and creates an output files in the C:\home folder.
However I want to move the output files to another folder autometically, lets say C:\test.
can it be done in a batch script?
Thanks in advance.
bla.bat
move c:\home\* c:\test
What is the problem ? DOS has a move command. Or you could simulate that with a copy and delete if move is unavailable for some reason.
You could save yourself the trouble of the batch using CMD redirects. Just paste the following behind the mdmg.cmd command.
> "C:\source i5\output.txt"
Basically the CMD interpreter will execute the command(s) in mdmg.cmd, and then redirect the output of the commands to print in output.txt. This way you don't have to call another batch. Another cool thing about doing it this way is that if the output file does not exist at the specified path, cmd.exe will make it for you.

Resources