Create subfolders and move files into their respective subfolder - directory

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

Related

Enter multiple directories, enter subdirectories, and move all files up one directory with bash

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

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

Make zip of directory contents and name zip same as directory - recursively

I have over 500 sub directories coming off of one root directory that each contain >6000 files each. the directories are named 20150218, 20150217, etc., one for each day of the year.
I want to develop a script that will zip all of the files in a directory, i.e. 20150217 and name the directory 20150217.zip. I then want to delete the original files.
so, all of the sud directories in ~/public_html/ispy/dlink/ would be zipped separately.
I appreciate any guidance.
Copy and paste following script into any unix editor (vim, geany, mousepad) and save it in directory with Your "date" subfolders. Name it as you wish, with no extension e.g. zipscript. From terminal: go to directory with Your script, allow to execute it: chmod +x zipscript and fire it: sudo ./zipscript.
#!/bin/bash
for D in *
do
if [ -d "${D}" ]; then
zip -r -j "${D}".zip "${D}"
rm -R "${D}"
fi
done
Script runs as follows: for every file (directory is also a file under unix) in current directory check if it is a directory and, if true, make zip with the same name, then delete subdirectory with all files in it.

Recursively search for a file name and copy in MS DOS

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

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