Copy batch file into newly created folder of same name - windows

I want to create a batch file which will search for .docx files in my folder and then for each of them create a separate directory and then copy each .docx file inside that directory. My folder contains of hundreds of files with so long names and housekeeping is a nightmare!
I have searched around and found some code which helps me to create a new directory for each .docx file. Here is the link.. "Win 7: CMD batch file for creating directories based on filenames"
The code which ONLY creates new directory for the file
for %%I in (*.docx) do mkdir "%%~pnI"
However how do I COPY the file INTO the newly created directory with the same name?
E.g file name is 20160611150424.docx (where numbers are YYYY MM DD HH MM SS) and using the code above it creates a file called 20160611150424.
How do i use batch file to copy this file into this folder? Which is the appropriate code to be used?
Many thanks,
NewLearner

Read copy /?: you could copy a file into newly created directory specifying the destination as a folder without file name and extension and copy command will retain them):
for %%I in (*.docx) do (
mkdir "%%~pnI" 2>NUL
copy /B "%%~fI" "%%~pnI\"
)
or specify the destination with file name and extension explicitly as follows:
for %%I in (*.docx) do (
mkdir "%%~pnI" 2>NUL
copy /B "%%~fI" "%%~pnI\%%~nxI"
)
Note that 2>NUL will suppress error message A subdirectory or file a already exists possibly generated by mkdir command if this is the case.
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%%~nxI etc. special page) Command Line arguments (Parameters)
(2>NUL etc. special page) Redirection

Related

Batch script to read and extract details from a text file

I need to copy a few files and folders to their respective destinations using a Windows batch script.
All the files and folders I am supposed to copy, are kept within a folder, SOURCE.
Example:
folder: C:\X\Y\Z\SOURCE\A
file : C:\X\Y\Z\SOURCE\A.txt
file : C:\X\Y\Z\SOURCE\B.txt
folder: C:\X\Y\Z\SOURCE\ZZZ
The destination paths of all the above are provided as text file contents, destination.txt.
Content of destination.txt:
C:\FinalDestination\D\A\...
C:\FinalDestination\N\A.txt
C:\FinalDestination\C\B.txt
C:\FinalDestination\U\ZZZ\...
Where three dots at the end signifies a directory, otherwise it's a file.
What I need to do in the above scenario is:
Copy folder A from SOURCE to C:\FinalDestination\D\
Copy file A.txt from SOURCE to C:\FinalDestination\N\
Copy file B.txt from SOURCE to C:\FinalDestination\C\
Copy folder ZZZ from SOURCE to C:\FinalDestination\U\
I don't know how to do it as I am pretty new to Windows command line.
I know XCopy is a command which can work for me, xcopy source destination, but I don't know how to extract the source and destination details.
Using an unchanged destination.txt and your supplied data, the following may help:
#Echo Off
Set "sD=C:\X\Y\Z\SOURCE"
Set "sF=destination.txt"
For /F "UseBackQ Delims=" %%A In ("%sF%"
) Do For %%B In ("%%~fA.") Do echo=XCopy/IY "%sD%\%%~nxB" "%%~dpA." 2>Nul
Pause
You need only modify the content of the variables at lines 3 and 4
Note:
I have currently made it so that nothing is copied, just the commands output to your screen. If you are happy with the output remove echo= from line 7 and delete the content of line, 8

dynamically adjust to the unzipped folder name in a batch file

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.

Batch: Copy File from different Subfolders with certain file name to other destination

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.

How to batch zip an image directory plus one js and one html file, Win 7

I would like to do a batch zip file operation. What each zip file has in common is an images directory and a *.js and a *.html file with the same name.
7zip has a pretty neat command line interface (which is also part of the 7zip documentation; see http://7-zip.org for more information) which can be used within a .bat file to create archives of any complexity.
For a start try the following batch file (e.g. save as generate.bat). It will loop over all subdirectories of the working directory and add all files of the subdirectories within its own .zip file named as the subdirectory. After that the corresponding .js and .html files are added to the archive.
#echo off
SET SEVENZIP=C:\Program Files\7-Zip\7z.exe
:: if 7z.exe is available within your PATH the following is sufficient: SET SEVENZIP=7z.exe
:: remember initial (current) directory
SET INITIAL_DIR=%CD%
:: define working directory
SET WORKING_DIR=%1
IF "%WORKING_DIR%" == "" (
ECHO No input directory given using current directory...
SET WORKING_DIR=%INITIAL_DIR%
)
:: change to working directory
CHDIR /D "%WORKING_DIR%"
:: loop over sub-directories
for /D %%a in (*) do (
ECHO.
ECHO.
ECHO *** Creating %%a.zip ***
:: add all files within images directory with name %%a
"%SEVENZIP%" a "%%a.zip" "%%a\*"
:: add .js and .html file
"%SEVENZIP%" a "%%a.zip" "%%a.js"
"%SEVENZIP%" a "%%a.zip" "%%a.html"
)
:: change back to original directory
CHDIR /D "%INITIAL_DIR%"
:end
ECHO.
ECHO.
PAUSE
To run it either copy it to your working directory or use the Windows Explorer and drag 'n' drop the working directory onto the .bat file.

How can I do a batch file to copy and rename a file?

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%

Resources