I'm having trouble setting the correct path to the folder containing my batch file. For example, right now, I have a zipped file called "example.zip". This zip file contains 4 files within it (file1, file2, file3, file4). When a user right-clicks and extracts the 4 files, they are given the option to rename the file path and folder name. By default, the folder name gets saved as "example". In my batch script, I can find and move the files great if they don't change the folder name. But if they change the folder name path to C:\Users\%username%\Downloads\"notexample" then it screws up the batch file.
I'm wondering how to grab the folder path after the user has extracted the the zip file and named it possibly something other than the default name.
My current configuration in my batch script is
for \f "delims=" %%F in ('dir /b /s "cd:~0,2%\Users\%username%\example" 2^>nul') do set filepath=%%F
This is just searching for any folder that matches "example" in the Users\Downloads directory and grabs the file path. You can see the problem if the user renames the folder "notexample". My batch script yells "folder not found"
Thanks
To get a batch file's location is quite simple, use the %~dp0 variable:
echo %~dp0
returns the batch file's folder.
That means you could simply use this:
set "filepath=%~dp0"
This won't require the for loop.
Related
Basically I have created a batch file which copies a file within FOLDER A and then renames the file with the date on the end and pastes it into FOLDER C.
Before this takes place I am looking to find out how I would set up a FIND function to search through FOLDER B to see if it has a file with the same name.
If it does contain the same name as the file in FOLDER A then I want it to copy that file add the date on the end and paste it into FOLDER C.
Lets say:
*wav file is in FOLDER A
FIND *wav file in FOLDER -B to see if it exists.
If the *wav file does exist
then copy this to FOLDER C
then re-name *wav with the date on the end
Hope this makes sense.
IF EXIST
cmd has a builtin IF EXIST that checks to see if a file exists, and the analagous IF NOT EXIST. (See IF /? or ss64.com) Something like this would do what you describe.
IF NOT EXIST "%TARGET_FILE%" COPY "%SOURCE_FILE%" "%TARGET_FILE%"
File/Path Parsing
cmd has some means of parsing paths, however, all the useful features are contained within the FOR command (See FOR /? or ss64.com).
I usually start with a typical environment variable, e.g. %SOURCE_FILE%, give it to FOR and allow FOR's substitution behavior to get either the parent dir, filename, extension, etc.
Suppose you wanted the base name of the file (no containing directory). You could accomplish that like this:
FOR /F "tokens=*" %%f IN ("%SOURCE_FILE%") DO SET "SOURCE_FILE_BASE=%%~ff"
Now, SOURCE_FILE_BASE would contain the base name of the file. (Note: the syntax of FOR changes when you use it in a script vs at the interactive shell. The use of %% is what a script needs.)
Combined
Based on what you describe, supposing you have a path to a file saved in SOURCE_FILE and the directory where you wish to copy the file to, TARGET_DIR, you could accomplish what you describe by doing something like:
FOR /F "tokens=*" %%f IN ("%SOURCE_FILE%") DO SET "SOURCE_FILE_BASE=%%~ff"
SET "TARGET_FILE=%TARGET_DIR%\%SOURCE_FILE_BASE%"
IF NOT EXIST "%TARGET_FILE%" COPY "%SOURCE_FILE%" "%TARGET_FILE%"
I am total beginner and need help with copying of files in windows. I have xlsx files in in different subfolders. The name of the files that I want to copy contain (but are not limited) to "Reporting Assessment Tool". I would like to copy all files containing that name and bring them to flat source destination (don't need the folder structure).
source: c:\users\name\desktop\upload
destination: c:\users\source\desktop\tool
So far I have tried the following
cd /D "c:\users\jubalkheimer\desktop\upload" #for /r %%a in ("*Reporting Assessment Tool.xlsx") do (
copy "%%a" "c:\users\jubalkheimer\desktop\tool\"%%~a")
I am not receiving a error message, but files don't get copied....
Can you help?
Many thanks
The batch file code for this task could be:
#echo off
for /R "%USERPROFILE%\desktop\upload" %%I in ("*Reporting Assessment Tool.xlsx") do (
copy /-Y "%%I" "%USERPROFILE%\desktop\tool\%%~nxI"
)
This could be done also with a single command line in batch file:
#for /R "%USERPROFILE%\desktop\upload" %%I in ("*Reporting Assessment Tool.xlsx") do #copy /-Y "%%I" "%USERPROFILE%\desktop\tool\%%~nxI"
The case-sensitive loop variable I holds on each loop run the name of a file with full path without surrounding double quotes found in the specified directory or any subdirectory of that directory matching the file name pattern *Reporting Assessment Tool.xlsx.
For copying the found file with prompting the user of the batch file for overwriting an already existing file with same name in target directory it is necessary to specify as first parameter the string assigned to the loop variable and as second parameter the target path and just file name and file extension of the found file without path. This explains the usage of "%%I" (file name with extension and full path enclosed in double quotes) and "Target Path\%%~nxI" (just file name and file extension with fixed destination path enclosed in double quotes) on COPY command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
for /?
%USERPROFILE% references the predefined environment variable USERPROFILE which holds the path to profile directory of the current user account.
It would be very easily possible to do this task also without the usage of a batch file using Windows Explorer.
Navigate to folder %USERPROFILE%\desktop\upload
Run in that folder a search for *Reporting Assessment Tool.xlsx
Windows Explorer automatically searches recursive in that folder and all subfolders for files matching this file name pattern.
Select all found files with Ctrl+A (select All).
Prepare for copying all selected files with Ctrl+C (Copy).
Navigate to folder %USERPROFILE%\desktop\tool
Paste the files prepared for copying with Ctrl+V (Paste) which results in copying the files now.
(I DIDN'T DO A BATCH WITH THIS STUFF YET)
I have a file called "worldcopy2.txt" which inside has a full directory:
C:\Users\Robert\Desktop\myworld2
And I want to create a batch file to change the inside of the .txt to only have the name of the last folder of the directory that be in the moment. For example:
If the .txt file says C:\Users\Robert\Desktop\funinside, the batch must change the .txt to say inside only funinside.
Must work with all the directories.
Translated with Google Translate.
I have tried this:
#echo off
setlocal
REM read a full path from file
type worldcopy2.txt
for /f %%F in (worldcopy2.txt) do set x=%%F
REM extract last path component and write it back to file
for %%P in ("%x%") do echo %%~nP> worldcopy2.txt
type worldcopy2.txt
Discard the type worldcopy2.txt statements after testing. The idea behind this is to treat the path as if it were a fully qualified filename and let the for loop extract the name. Note that this will fail if the foldername contains a dot (".").
I am trying to create a batch file to copy a "folder" as a backup to a new destination. However, I need to add a date stamp of the file name.
for example folder is myFolder when I copy it I want to to be named myFolder_todays_date
I want to be able to copy the folder and it's content.
I found the xCopy but I am not sure how to write it correctly.
now I tried xCopy /e/i dir newDir
How can I append the date to the folder name?
I appreciated your help on how to write this batch file correctly.
Thanks
#echo off
mkdir %1%DATE%
xcopy /s %1 %1%DATE%
This accepts the name of the folder as a command line argument.
For example if the above batch file is called bkup.bat and you want to make a backup of a directory called work, you run it as
bkup.bat work
It copies it into the current directory. You can accept a 2nd command line argument (%2) & also use that if you want to copy to a different directory
xcopy /s %1 %2\%1%DATE%
Think I have a compressed file named "Target.rar" and its location is Desktop, inside it I have put a batch script. When I open the rar file (without extracting it) I can see the files inside. If I double click my batch script, winRAR first extract all files into temp folder and will run my batch script from there, so the root directory will be Temp folder, But I need to do something on .rar file location (which is desktop here).
How to get real location of Rar File?
You can search for the rar file if you are confident that you know there won't be any other files with the same name.
dir /b /s C:\Target.rar
Obviously if you know how deep you have to look you can narrow down the search path, and save some time.
If you know it will at least be under C:\Users\%username% then you can use
dir /b /s C:\Users\%username%\Target.rar