How to delete subpath in batch file? - batch-file

How can I edit path which i get from command line arguments and delete last subdirectory?
e.g. I run my batch file with argument:
C:\Users\Aga\Desktop\something
and I want to use only
"C:\Users\Aga\Desktop"
part in my batch file.

Add this as the very first line of your batch file:
#If Not "%~dp1"=="" #(Set "Parent=%~dp1"&Call Set "Parent=%%Parent:~,-1%%")
Then use %Parent% throughout your script as necessary, (%Parent% will be the next directory up the tree if the input was a directory or the container folder if the input was a file).

Related

Batch file to run cmd command

I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"

copy the path specified in the text file to the source link of anothe text file using a bat file

I am doing a automation testing, my content is before start of test. I will copy the copying content path in a text file and this path one by one will has to be linked to the source path of the another bat file., this is the concept:
A text file containing paths specified line by line, I need to copy the first line of this path file to the another text file where this copied first line will be act as a source file path.
I am new to this.
It sounds like you wish to do something along these lines, but it would be useful to hear more of what you're hoping to achieve.
for /f %%f in (textfile.txt) do call process.bat %%f
This line reads each line from textfile.txt and passes that line into process.bat for it to do something with that line. Save this code into a .bat file and create some other .bat file called process.bat (your subroutine). And change the name of textfile.txt above to your expected file. Or pass that in to the .bat file and use %1 to access it.

Batch file to move a file from a folder to another folder & create a text file in its place having the name of the moved file

I needed a batch file to which we could provide input (any file) from Windows 7 context menu option "Send to".
This batch file must then move that file to another folder, & create a text file in it's place with the same name as the moved file.
I needed this to move mp3 files from their album folder to a common folder, while keeping a track of which file belonged to what folder.
At first create a Batchfile with the following content:
#echo off
set newpath=H:/testing
set filename=%*
move %filename% %newpath%
set txtfilename=%filename:~0,-3%txt
echo.content of textfile >%txtfilename%
where insteadof the H:/testing you put the new path of your files,
and instead of the "content of textfile" you write what shall be in the textfiles created at the old location of the files.
it does not matter where you create the batch file, because in the next step you go to the search bar and type in shell:sendto and open the folder,
where you create a shortcut to your batchfile.
You can now send files to your batchfile over the "send to" menue,
and the batch file copies the file to the specified path and leaves a text file with the desired content where the moved file once was.
Edit: if you want to use it on files with varying number of characters in the ending, the code has to be modified to
#echo off
setlocal enabledelayedexpansion
set newpath=H:/testing
set filename=%*
move %filename% %newpath%
set ending=%filename:*.=%
set txtfilename=!filename:%ending%=!.txt
echo.content of textfile >%txtfilename%
but in this case you can't have dots or file-endings in the names of your folders or leaving the textfile wont work properly.

Start command cannot locate file from batch but can from Command Line

I am writing a startup script to open a .pdf file. When I execute the command in cmd window, the .pdf will open just fine, but when I execute the command from a .bat file, it says "Windows cannot find 'myfilename.pdf'. Make sure you typed the name correctly, and then try again."
The command I am using is
start myfilename.pdf C:\Temp
Not really sure what I should change other than perhaps insert change directory to C:\Temp before executing the start command?
When you run your command from the command line I assume you are in the same directory as the pdf file. That is why the file is found.
When you run a .bat file, the starting path is the path of the .bat file. If you have the .bat file in the same directory as the .pdf file, your command will work. If you have the .bat file in a different directory you can first change the current directory to the one that contains the .pdf file, or give the full path to the file like below:
start C:\LocationOfPdfFile\myfilename.pdf C:\Temp
If your batch file is not in the same directory as your file, it cannot be opened. If you specify the path it does not matter in what directory you are.
if exist C:\Temp\myfilename.pdf (
rem file exist an is being opened
start C:\Temp\myfilename.pdf
) else (
rem file doesn't exist
)

How to append filename to current directory in Batch file?

I want to search a file in the current directory from which the batch is running, append the filename to the directory and include that entire directory as part of command that.
So.....
Directory:
C:\tempfiles\batch
Files in C:\tempfiles\batch
tmp1.txt
tmp2.txt
tmp3.txt
anyname.exe
I want the batch file, run from the directory, to find any .exe file and append it to the directory name, and use that new string as part of a command to copy the .exe file over to another directory. The command will eventually read like this (the FILETRANSFERSW.exe is the file transfer software that is also in the directory):
C:\tempfiled\batch> FILETRANSFERSW.exe "%CD%\tmp4.exe" X:\dest
The .exe file name will be changing so i need to dynamically add the new filename into the above command everytime i run the batch file. Any ideas??
If I read your problem correctly, is it sufficient to use the "for" keyword?
for %a in (*.exe) do FILETRANSFERSW.exe %a X:\dest
You can test the output with something innocuous like:
for %a in (*.exe) do echo [[%a]]
%a ends up iterating over *.exe in the current directory, returning the full file name for each one.

Resources