Recursively search for a file name and copy in MS DOS - recursion

I have a text file containing names of files separated by newline and a folder with many sub folders which would contain the files matching with names in text file.
I want to pick file names from text file which can be done using for loop; and recursively search for file name in the folder and if the file is found copy it to a different location.
Can anyone please shed a light on it?
Thanks,

#echo off
for /f "usebackq delims=" %%a in ("file names.txt") do (
for /f "delims=" %%b in (' dir "c:\folder\%%a" /b /s /a-d ') do (
copy "%%b" "c:\new folder"
)
)

Very easy (though you'll have to a bit more specific so you can tweak the code to suit your situation.
Base Code:
#echo off
for /f "usebackq tokens=*" %%a in ("file names.txt") do (
forfiles /p "C:\users\...[path to main file]" /s /m "%%a" /c "cmd /c copy #path "C:\users\...[target path]""
Not sure if the above double quotes will stuff up, if it does then we can replace with a call and enableextensions.
Tell me if that doesn't work (since it will only work on Win7). Because there are many other ways to do it.
Mona

Related

How to Delete an Entire Folder with Names Beginning with Numeric Characters

Dear StackOverflow community,
Could you please recommend a batch script to solve the following issue:
There are numerous subfolders in the KApps Folder that begin with numeric characters.
The path is as follows: %LocalAppData%\KApps
1a26745; 2bah257,... are subfolders.
I want to delete all of those subfolders.
Looking forward to receiving your assistance.
I attempted to use this batch script, but it did not work.
#cd /d %LocalAppData%\KApps
for /d %%f in ([0-9]*) do rd /s /q "%%f"

Running a Sqlite batch file to read all .txt files in the same directory

Problem is i need to run a batch file that will run all file with .txt extension one at a time, but right now i can only run 1 file manually..
sqlite3 db1.db ".read 1.txt"
saved as a .bat file
what i would like,is for it to read all txt files in the directory because it only works for the above named txt file
Updating answer. Try concatenate your text files, then do the command.
copy /b *.txt newfile.txt
sqlite3 db1.db ".read newfile.txt"
---You said that may not work because files are too large then, try this as an alternative?
:start
setlocal EnableDelayedExpansion
for /f "tokens=* delims=" %%a in ('dir/b "%~dp0\*.txt"') do (
set file=%%a
echo "!file!"
call :readfile !file!
)
ENDLOCAL
:readfile
sqlite3 db1.db ".read %file%"
pause
:exit
goto :EOF

Delete dos folders 4 chars long starting with letter S

I am trying to find a command, which could be described like
rmdir s???
i.e. delete all folders in current dir starting with S, exactly 4 characters long. I've tried rmdir, del, erase, none of them works.
Any ideas please? Will it be different for empty and non-empty folders?
In MS-DOS 6.0 (per your question):
deltree/y s???
And in case you are not in MS-DOS but actually on the Windows command line:
for /f %i in ('dir /a:d /b s????') do rd /s /q %i
I've found something similar to the first answer (in case of empty folders just remove the /s switch)
for /d %n in (s???) do rd /s "%n"

Create subfolders and move files into their respective subfolder

Is it possible using command prompt to read the file list in a given directory, such as:
C:\Users\My Documents\Folder1\
Which contains the following files:
file1.txt
file2.txt
file3.txt
...
Create a subfolder for each file, named for each file name
C:\Users\My Documents\Folder1\file1\
C:\Users\My Documents\Folder1\file2\
C:\Users\My Documents\Folder1\file3\
...
Then move (or copy) all the files into their respective subfolder? The end result would be files existing at:
C:\Users\My Documents\Folder1\file1\file1.txt
C:\Users\My Documents\Folder1\file2\file2.txt
C:\Users\My Documents\Folder1\file3\file3.txt
...
this is what i ended up using:
#ECHO OFF
for %%F IN (*) do (
echo Copy local file: %%F
mkdir "%CD%\%%~nF\"
copy /Y "%%F" "%CD%\%%~nF\%%F"
)
rd /s /q "%CD%\MoveToSubFolders"
ECHO.Done
i hope this may help you
md folder1
for i=1 to numberOfSubfolders
md folder+i
cd folder+i
create filer+i
cd..
next

Copy Files recursively from inside directory to outside using batch file

There is this directory structure:
Dir1
--Dir2
--File1
--File2
--Dir3
--File3
--File4
--File5
Now I want to copy all the files in subdirectory (Dir2, Dir3) to parent Directory Dir1 using batch files.
I have come up with the below code but it doesn't work perfectly. I get the below output -
Directory2 --It has 4 files all together
Invalid number of parameters
Invalid number of parameters
Does E:\Directory1\Copy\File1.dat specify a file name -- And only this file gets copied
or directory name on the target
(F = file, D = directory)?
Code -
#echo off
call :treeProcess
Pause
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for /D %%d in (*) do (
echo %%d
cd %%d
for %%f in (*) do xcopy %%f E:\Movies\Copy\%%f
call :treeProcess
cd ..
)
exit /b
No need for a batch file. Execute the following command from your Dir1 folder:
for /r /d %F in (*) do #copy /y "%F\*"
As a batch file
#echo off
for /r /d %%F in (*) do copy /y "%%F\*"
BUT - Be aware that you may have the same file name in multiple child folders. Only one will survive in your Dir1.
EDIT
The above assumes you are running the command, (or the script) from the Dir1 folder. It can be run from anywhere if the script is augmented to include the path to Dir1.
for /r "pathToDir1" /d %F in (*) do #copy /y "pathToDir1\%F\*"
or as a batch file
#echo off
set "root=pathToDir1"
for /r "%root%" /d %%F in (*) do copy /y "%root%\%%F\*"
You could pass the path to Dir1 as an argument to the batch file. Pass in . as the path if you want to use the current folder.
#echo off
for /r %1 /d %%F in (*) do copy /y "%~1\%%F\*"

Resources