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%"
Related
I have something like saved a file here:
C:\Test\test.txt
I now want to move them via CMD so that the path is as follows:
C:\test.txt
Is that possible?
The command is:
move c:\test\test.txt c:\
The first argument is the source file.
The second argument is the target file or target-directory.
IF you just want to move the file exactly one level up the tree, and you don't know the name of the target directory, then you can use the .. indicator which means the parent-directory .
example:
move c:\Test\test.txt ..
will move the file into c:\Test .
If any part of the path or filename contains spaces then double-quote the source and/or target name appropriately.
See the help for the move command help move.
Yes, it is possible to move one or more files or folders up one level in folder hierarchy by using .. as described by Microsoft in the documentation about Naming Files, Paths, and Namespaces.
The simple approach in a command prompt window would be:
for %I in ("C:\test\test.txt") do for %J in ("%~dpI..\") do move "%~I" "%~fJ"
The command line in a batch file would be:
for %%I in ("%~1") do for %%J in ("%%~dpI..\") do move "%%~I" "%%~fJ"
%~1 references the first argument passed to the batch file which can be a file/directory name without or with a relative or with an absolute path, or even a wildcard pattern to move multiple files/directories up in the directory tree. The files/folders can be passed to the batch file also with a UNC path.
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.
for /?
move /?
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.
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 writing a batch file that uses some of the files within it's parent directory (lets say Folder1).
C:\User\Steve\Foder1\
Now I want to make the whole Folder_1 relocatable so that I can copy paste the folder anywhere on my/someone else's computer and run batch script.
D:\User\Random_guy\Folder1\
The question is how do I start batch file's command prompt to (D:\User\Random_guy\Folder1) it's parent directory without writing another batch script to do that.
Start your batch file with:
pushd %~dp0
That will set the current directory to be the folder containing the batch file. Then in the batch file, make sure all your paths are relative to the current directory.
However, if your batch file changes to other directories in the course of its execution and you would still like to be able to refer to the batch's home folder's contents without knowing the exact path, use the same %~dp0 as the path to the file(s) you want to use. For instance, a FileA from the same folder as the batch file would be addressed as
"%~dp0FileA"
Note the absence of a \ before the FileA. This is because the %~dp0 already includes a trailing \ and so the entire thing will evaluate to a correct path all right. (Although if you do put another backslash, like "%~dp0\FileA", it should work as well, because Windows usually disregards multiple consecutive backslashes when in the middle of a path.)
%CD% in a batch file gets the current directory/directory batch is being run in.
So - if I've got this right, you want to run
`C:\steve\folder1\yourbat.bat`
which should copy C:\steve\somefiles to D:\Random_guy and set the current directory to D:\Random_guy\folder1 ?
#ECHO OFF
SETLOCAL
SET subdir=folder1
SET destdir=%~1
IF NOT DEFINED destdir ECHO Require destination username&GOTO :EOF
SET destdir=%~1\%subdir%
:: Ensure destination exists
ECHO MD "%destdir%" 2>nul
SET "sourcefrom=%~dp0.."
ECHO COPY "%sourcefrom%\filestocopy" "%destdir%\..\"
ECHO CD /d "%destdir%"
Note that the MD, COPY and CD commands are merely ECHOed, you'd need to remove the ECHO keyword to execute them.
Run the batch with a parameter of D:\Random_guy