Batch file: moving specific files from subfolders to a destination folder (Windows) - windows

I'm new to writing code / commands, and I was hoping for some assistance.
I need to search and copy specific files within a folder and its subfolders.
I have the following script (that I use when all of the files are in one source folder):
#echo off
set src_folder=C:\Users\macca\Downloads
set dst_folder=C:\Users\macca\Downloads\Testing
set file_list=C:\Users\macca\Downloads\photo_test.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)
pause
I've tried to google this, and search on stack overflow, but I'm not finding anything (I suspect that I am using the wrong terminology) .

Related

How to auto create folder based on filename and move the file into it's folder using .BAT

I have already solved my question... What I haven't solved is how to do this if the .bat file is located in a parent folder and that it should work on all subfolders?
Right now, there's a limitation that it only create folders if the .bat file is located in the same folder as the files. It can't create folders if the files are inside a subfolder.
What I have is:
the filename of this .bat is :
organize.bat
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
How I do it right now:
I place the .bat file in a folder together with the files
When I click it, it will create folders with a name based on the files inside that folder
It will also move each files in those folders of the same name
What I need:
Place the .bat file in the main folder with many subfolders containing the files
Click it to perform the same tasks above
Apologies if my explanation is confusing... I hope it's still understandable.
Thank you in advance!
Your attempt is very close to working but beware the wrinkles of using a simple approach without checking each detail so, start here:-
#echo off & Title %~n0
REM I recommend using cd /d "%~dp0" to ensure you start from the known cmd file folder location not some system folder
cd /D "%~dp0"
REM add the /R switch to run through subdirs
for /R %%i in (*) do (
REM replace organize to %~n0 so as to aid renaming by other users
if not "%%~DPni" == "%~DPn0" (
REM to allow for nested paths we need a fuller DP location for N (check it works exactly as desired then remove the echos)
echo md "%%~DPni" && echo move "%%~i" "%%~DPni"
)
)
BEWARE files with double .dot.s such as cmd.exe.lnk so check those echo's first
md "C:\Users\me\Favorites\Links\cmd.exe"
move "C:\Users\me\Favorites\Links\cmd.exe.lnk" "C:\Users\me\Favorites\Links\cmd.exe"

How to Create Conditioned Subfolder through batch file?

We are using Jenkins as our CI tool. At present, I have written a batch file, which takes build value from a notepad file (written by developer), and then copies it to the network drive with the build value as a folder.
The batch file is mentioned below
for /f "tokens=2" %%i in ('findstr Build "C:\Program Files (x86)\Jenkins\jobs\**\build.txt"') do set Build=%%i
set Build=%Build:'=%
if not exist "\%Build%\" mkdir "%Build%\"
My motive now is that, if the value in notepad is 6.1 or 6.a, i.e anything after decimal,it doesn't create folder, but places it in 6 subfolder. Alternatively, if the value is full, it creates a parent folder.
The text file, where I get the build number is mentioned below.
#define MyAppVersion '4.0.0.0'
#define MyFullAppVersion '4.1.0.0'
#define BuildNumber '81'
I need to create folder on the basis of value entered in "Build Number". If the value is in decimal, it should create within subfolder, else there should be only a main folder.
With the help of below input, I created below batch file for achieving this, but it didn't worked.
for /f "tokens=2" %%i in ('findstr Build "C:\Folder Check\logs\build.txt"') do set Build=%%i
set Build=%Build:'=%
for /F "tokens=2""delims=." %%j in ('findstr Build "C:\Folder Check\logs\build.txt"') do set "sub=%%j"
if exist "\\network\%Version%\%Build%\" mkdir "\\network\%Build%\%sub%%"
if not exist "\\network\%Version%\%Build%\" mkdir "\\network\%Version%\%Build%\"
if exist "\\network\%Version%\%Build%\%sub%%j" XCOPY /y "C:\Program Files (x86)\Notepad++\readme.txt" "\\network\%Version%\%Build%\%sub%\" /E /S
if not exist "\\172.19.0.4\Departement$\Development\Development RCX\RCX_M02_CSP\INTERNAL RELEASES\CI\%Version%\%Build%\%sub%%" XCOPY /y "C:\Program Files (x86)\Notepad++\readme.txt" "\\network\%Version%\%Build%\" /E /S
Any help will be greatly appreciated.
You could use the following code to split off the period . and everything behind:
for /F "delims=." %%j in ("%Build%") do set "Build=%%j"
Take also a look at the quoted set syntax, which is the one I recommend in general, because no special characters could cause problems that way.
Your if exist syntax appears a bit odd to me; I am not sure where the directory %Build% should be located, because you precede a \ during existence check but you do not during creation. If you want to ensure the directory exists in the current working directory, do not precede the \:
if not exist "%Build%\" mkdir "%Build%"
If you want it to be located in the root directory od the current drive, then do precede it by \, but this is not what you want, I guess.
However, the appended \ is needed to check a directory for existence; not providing it would also check for files.
Anyway, to ensure a directory exists, you do not need to check for existence, you can just create it and suppress the error message that appears in case the directory already exists, like this:
mkdir "%Build%" 2> nul
I am not sure if I interpret the ** in your path specification right, but I assume you are going to let me know...
You can use global wild-cards (*, ?) only in the last element of a path, so C:\Program Files (x86)\Jenkins\jobs\**\build.txt is not valid. In case there is only a single directory in C:\Program Files (x86)\Jenkins\jobs\, you can get it like this:
for /D %%D in ("C:\Program Files (x86)\Jenkins\jobs\*") do set "SubDir=%%~D"
If there are multiple directories and you want to get the last modified one, you could use this:
for /F "eol=: delims=" %%D in ('
dir /B /A:D /O:D "C:\Program Files (x86)\Jenkins\jobs\*"
') do set "SubDir=%%D"

How to copy files in specific location within multiple folders to a shared drive using windows command?

I need to copy several backup files to a shared folder
The Source to copy from looks like this below (there is other files inbetween but are not interested in copying them
C:\BackupLocation\BCK1\Logs\myLog1.log
C:\BackupLocation\BCK1\Logs\myLog2.log
C:\BackupLocation\BCK1\Logs\myLog3.log
C:\BackupLocation\BCK2\Logs\myLog1.log
C:\BackupLocation\BCK2\Logs\myLog2.log
C:\BackupLocation\BCK2\Logs\myLog3.log
C:\BackupLocation\BCK3\Logs\myLog1.log
C:\BackupLocation\BCK3\Logs\myLog2.log
I need to be able to copy any log file with the words "myLog" in the file name.
I thought about doing a For look do get the directory and then using robocopy to copy the files to my shared location
FOR /d /r %ROOT_SRC_BACKUP_LOCATION% %%G in (*logs*) DO (
Pushd %%G
Robocopy %%G "mybackup location directory" *mylog* /S
Popd )
But the problem is that the logs directory detects other logs directory... for example I have another logs directory in here
C:\BackupLocation\BCK3\server\Logs\IDontCareAboutTheLogsIntheServerDirectory.log
What is the most effective way to copy all the myLog's in the C:\BackupLocation\BCKXX\Logs\ folder?
Test this:
#echo off
Pushd "C:\BackupLocation"
for /d %%a in (bck*) do (
if exist "%%a\logs\" Robocopy "%%a\logs" "d:\mybackup location directory\%%a\logs" "*mylog*.log"
)
Popd

Batch file to create subfolder in other subfolders, but only at top level

I have a similar problem to this post, but I couldn't get the solution to work (I'm a total novice).
I have a set of folders on a network drive, each folder named after a manager (e.g. \sfnetfile\DEPT\HR_Manager\Manager, Test and \sfnetfile\DEPT\HR_Manager\Manager, Test 2). I am trying to get a folder/subfolder combo created in each manager folder of 2013\Variable. In some cases managers already have a 2013 subfolder, but not the Variable sub-subfolder (excuse the silly terminology).
I also want to prevent the batch file from creating the 2013\Variable folder in existing other subfolders within each manager folders (so I don't want it, for example, creating another level down: \sfnetfile\DEPT\HR_Manager\Manager, Test 2\2012\2013\Variable.
Here is my code:
FOR /d %A IN (\sfnetfile\DEPT\HR_Manager*) DO mkdir "%A"\2013\Variable
Something like this should do:
#echo off
for /d %%d in (\\sfnetfile\DEPT\HR_Manager\*) do md "%%~d\2013\Variable" 2>nul
assuming that sfnetfile is the name of a remote server and DEPT is a shared folder on that server. If sfnetfile is a top level folder on a mapped network drive, change that to:
#echo off
for /d %%d in (X:\sfnetfile\DEPT\HR_Manager\*) do md "%%~d\2013\Variable" 2>nul
where X: is the drive letter of the network drive.
Edit: As #dbenham correctly mentions, redirecting error output (to suppress error messages for existing leaf folders) will also suppress any other error message. A more sophisticated approach would check if the folder doesn't already exist and only then create it:
#echo off
setlocal EnableDelayedExpansion
for /d %%d in (\\sfnetfile\DEPT\HR_Manager\*) do (
set "folder=%%~d\2013\Variable"
if exist "!folder!" (
if not exist "!folder!\" echo "!folder!" exists as a file! 1>&2
) else (
mkdir "!folder!"
)
)

To copy entire subfiles to other (only one) folder without path in windows platform

I did lots of search however i couldnt achieve what i want to do :
I have a folder which contains lots of folders and files under it. I just want to copy entire JPEG files in this folder to another folder without copying previous path tree.
I used this batch command like that :
for /r "C:\source" %%f in ("*.JPG") do #copy %%f "C:\dest" /Y
It works some files which dont contain any space in the name of files. However, it doesnt work files which contains space in file name like that :
it can find and copy "aabb.JPEG"
it can not copy "aa bb.JPEG"
any solutions?
The following works great for me
#ECHO OFF
FOR /R "c:\source" %%F IN (*.jpg) DO (
copy "%%F" "c:\dest\" /Y
)

Resources