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\*"
Related
I have multiple directories like:
T1_5356
T1_5357
T1_5358
.
.
Each directory has a sub-directory called DTI
I would like to enter each directory and copy the contents of its respective DTI contents up one directory (so cp T1_5356/DTI/* ../)
I have been trying variations of:
for dir in T1*; do cd $dir; cp -r DTI/*; cd ../; done
but I get an error:
cp: cannot stat `DTI/*': No such file or directory
from inside the directory that contains all the T1_* directories:
for dir in T1_*; do cp $dir/DTI/* $dir; done
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
I can get a list of Qt .ui files like so:
D:\programing\qtproject\ui\designer>dir *.ui /B
main.ui
mainmenu.ui
mainwindow.ui
And what I do right now is that I manually run uic for every one of them, because IDE I'm using (QtCreator, ironically the one IDE that should be perfectly compatible with Qt) is not capable of that.
So can I run uic with a list of files obtained from dir *.ui /B? I would prefer if this worked recursively on all subdirectories.
Ok, I cracked it using some helpful answers:
#echo off
rem Runs uic command over all files in all subdirectories
rem For loop arguments explained here: http://stackoverflow.com/a/3433012/607407
rem start command arguments explained here: http://stackoverflow.com/a/154090/607407
rem /B is why the start creates no additional windows: http://stackoverflow.com/a/324549/607407
for /f %%F in ('dir *.ui /S /B') do start "" /B "uic" %%F -o %%~dpFui_%%~nF.h
To disable the recursion (subdirectories), remove the /S parameter in the dir command.
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
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