how to find the subfolder files in cmd? - batch-file

I have tried the below code to copy the list of files in the myfile.txt file. The result files copied the current directory only but I want to copy all subfolder files which I mentioned in the file name on the text file. Is there a way to copy the files from subfolders?
myfile.txt contains the list of file names only.
#echo off
for /f "delims=" %%L in (G:\MyFile.txt) do copy "%%L" G:\

Related

Batch extract multiple archives that skip the first folder in the archive and overwrite all files into a single directory

I have about 150 folders with .7z files that I want to extract all the .7z files in order into a different directory, "Z:\master statistics" and automatically overwrite. Below is the batch I'm working with, but I am unsure how to extract the .7zfiles and skip the first parent folder in the .7z.
For example:
sales2015.7z file has the below directories
sales/engines/parts
sales2010.7z file has the below directories
sales/equipment/parts
When it extracts, I am trying to get it to extract from the second directory. So if the batch worked correctly, the output directory would only have the Engine & Equipment folders and all its subdirectories. If a file with the same name exists, it automatically overwrites.
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
FOR %%X in (*.7z) DO (
"C:\Program Files\7-zip\7z.exe" x -o "%Z:\master statistics%"
)
popd
)

Copying and renaming multiple files in a .bat file with FOR ... DO xcopy

I want to move several thousand files (.jpg and .pdf) from one location to another and then rename each file.
My 'copy_rename_docs.csv' input file contains two columns:
the first column containing the original path AND file names for each file that I want to move and rename
and the second column containing the new paths AND file names that I want to move and rename each file to
like this:
\\myapp\data\e3\9b\e39bf5e8d745833418d3edcd97c6c7589716970b,\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\data\09\f5\09f58d1a1345f67a36154e99ffedb27308fa4292,\\myapp\test_migration\09\f5\nxf7.pdf
so "e39bf5e8d745833418d3edcd97c6c7589716970b" is a SHA1 hash of "Test File-xxx - SS1.pdf"
To copy and rename the files I tried this batch file:
#echo off
FOR /F "tokens=1,2 delims=," %%a IN (copy_rename_docs.csv) DO (xcopy /e /i "%%a" "%%b\*")
pause
exit
But instead of copying and renaming the files to this:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf
It is creating a FOLDER for each file with the FILE names as the FOLDER names and copying the files with their original file names into each FOLDER, like this:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf\e39bf5e8d745833418d3edcd97c6c7589716970b
\\myapp\test_migration\09\f5\nxf7.pdf\09f58d1a1345f67a36154e99ffedb27308fa4292
What I want is:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf
What's wrong with my batch file?
And is xcopy the best tool for this?

How to get a list of files and folders of just those folders listed in a text file into a list file?

I created a batch file to export all files, folders, and subfolders in a specified directory to a text file. What I need is to list specific files with subfolders from a text file which includes their path. Batch file will list files and subfolders only included in this text file. For instance, I need to export to a text file only files under those folders as in the input text file.
C:\Users\Username\Documents\test1
C:\Users\Username\Documents\test2
C:\Users\Username\Documents\test3
C:\Users\Username\Documents\test4
Thanks in advance.
Edited to add more details; normally, we use this command to list all files under a directory:
dir > output.txt
I want to apply another approach. I'd like to list files under particular directories only that a text file will include those specific directory paths.
This is text file:
C:\Users\Username\Documents\myfolder1
C:\Users\Username\Documents\myfolder2
C:\Users\Username\Downloads\yet another folder
Although there are many other folders inside Documents and Downloads, only files under folders whose paths listed in my input text file will be listed. Then, all subfolders and files of myfolder1, myfolder2, and yet another folder will be sent to an output text file.
Read a file containing folder names and list files in each of those folders and its subfolders:
#echo off
for /f "delims=" %%a in (file.txt) do dir /s /b "%%a\*"
Adapt the dir switches and file mask to your needs.
This very simple batch file can be used for this task:
#echo off
rem First check existence of folders list file and exit
rem batch file execution if this file does not exist.
if not exist "%UserProfile%\Documents\FoldersList.txt" goto :EOF
rem Delete output list file if already existing from a previous execution.
rem The error message output on file not existing is suppressed by
rem redirecting it from handle STDERR (standard error) to device NUL.
del "%UserProfile%\Documents\OutputList.txt" 2>nul
rem For each folder path in folders list file run the command DIR to output
rem in bare format all files and folders including hidden files and folders
rem in the folder and all its subfolders with redirecting the output written
rem to handle STDOUT (standard output) to the output list file with appending
rem the new lines at end of the list file. The list file is automatically
rem created on first line written to the output list file.
for /F "usebackq eol=| delims=" %%I in ("%UserProfile%\Documents\FoldersList.txt") do dir "%%~I\*" /A /B /S >>"%UserProfile%\Documents\OutputList.txt"
Just a list of files with no folders is produced with replacing DIR option /A by /A-D which means any attribute except attribute directory. That would be the same as using for last command line with the difference that files with hidden attribute set are not output by inner FOR:
for /F "usebackq eol=| delims=" %%I in ("%UserProfile%\Documents\FoldersList.txt") do for /R "%%~I" %%J in (*) do echo %%J>>"%UserProfile%\Documents\OutputList.txt"
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.
del /?
dir /?
echo /?
for /?
rem /?
See also the Microsoft article about Using command redirection operators.

CMD command to copy only files and not folders in a folder tree

Let's assume the following folder structure:
c:\tab\file1.txt
c:\tab\insidetab1\file2.txt
c:\tab\insidetab2\file3.txt
c:\tab\insidetab3\insidetab4\file4.txt
I want to copy only files file1.txt, file2.txt, file3.txt, file4.txt to some destination. Basically I want to remove all folders and keep only the files. Is it even possible?
This will copy all files that match the pattern file*.txt in C:\Tab to C:\Newtab.
for /R C:\tab %%G in (file*.txt) do copy %%G C:\newtab\

Print all the files inside a dir through batch file

I want to write a batch file in DOS which reads all the files from the input directory and display the list of files on the screen using FOR loop.
dir for the files in the directory
dir /s if you want to include the files in the sub-directories.
microsoft command line reference
edit:
FOR %%i IN (*.*) DO echo %%i
This will print out the names of all the files in the directory you run the batch file in.

Resources