Copy User Subfolders Using Robocopy - cmd

I am trying to copy the Normal.dotm from all user's C:\Users\%username%\AppData\Roaming\Microsoft\Templates to C:\Temp\%username%. I'm specifically trying to backup the data before a reinstall as part of an SCCM Task Sequence. Any thoughts?
I have tried a few different scripts using robocopy and it either gets stuck in a loop or only copies one directory.
robocopy C:\Users\%username%\AppData\Roaming\Microsoft\Templates\. C:\Temp\%username% /s /create
Only copies directory cmd is run as:
robocopy C:\Users\ C:\Temp\ /s /xjd normal.dotm
Creates loop and creates C:\Users\Application\Data\Application Data\ forever

The account running this will need to be an Administrator in order to access everyone's directories. When you believe the correct commands are being created, remove the lower case echo from them.
#ECHO OFF
FOR /F "delims=" %%f IN ('DIR /S /B "C:\Users\Normal.dotm"') DO (
SET "TDIR=C:\temp%%~pf"
IF NOT EXIST "%TDIR%" (echo MKDIR "%TDIR%")
echo COPY "%%~f" "%TDIR%"
)

You might use a different way:
#echo off
setlocal EnableDelayedExpansion
for /R "C:\Users\" %%A IN (Normal.dotm) do (
set "fpath=%%~fA"
if not "!fpath:\AppData\Roaming\Microsoft\Templates\=!" == "!fpath!" (
rem Find username:
for /F "tokens=3 delims=\" %%B IN ("%%A") do (
set "current_username=%%B"
)
rem Copy files:
copy "!fpath!" "C:\Temp\!current_username!\"
)
)
That, of course, requires administrator privileges, you cannot enter other users directories without admin privileges. Right-click on your file and select 'Run As Administrator'.

Related

Open a folder using User input in CMD

At my office we have to regularly open many different job folders on the fly at the same time. I was wondering if there was a script that could allow a user to input the folder name in CMD which will search specified directories on the network drive to open that folder in explorer.
An Example of the folder structure is below:
G/Work_A/2017:
Folder_A
Folder_B
Folder_C
Folder_D
G/Work_A/2018:
Folder_E
Folder_F
Folder_G
Folder_H
(I would like to search both 2017 & 2018 folders)
Also all the folders that users input have unique names.
Thanks for the help !
It is a bit unclear what you want hence why my and #npocmaka answers differ a bit :)
Regardless, This will prompt a user for a foldername, then do a search for that folder and open the folder or all instance of the folder on G:\ it can be amended to do all drives as well or limited to specific directories.
The point that stood out is the fact that you say each folder is unique, meaning each should only exist once on G:\ so the first scenario should work.
#echo off
set /p promptfolder="Please type the folder you wish to open: "
for /R G:\ %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
if you want to specifically limit it to only the 2017 / 2018 directories:
#echo off
set /p promptfolder="Please type the folder you wish to open: "
for /R G:\Work_A\2017 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
for /R G:\Work_A\2018 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
To add it as a loop, to keep the batch open purely use goto
#echo off
:start
cls
echo Last folder requested: %promptfolder%
set /p promptfolder="Please type the folder you wish to open: "
for /R G:\Work_A\2017 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
for /R G:\Work_A\2018 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
cls
goto start
I'm not sure what exactly you want but you can try this:
#echo off
echo --Choose folder to open--
echo G:\Work_A\2017
(echo()
dir /b /a:d G:\Work_A\2017\
echo G:\Work_A\2018\
(echo()
dir /b /a:d G:\Work_A\2018\
set /p "year=Year(2018/2017):"
set /p "folder=Folder:"
if exist "G:\Work_A\%year%\%folder%\" (
explorer "G:\Work_A\%year%\%folder%\"
)

Batch to move files outside of randomly-named subfolders

I have a data set consisting of files organised according to the following hierarchical folder/subfolder structure:
I would like to remove all nuisance subfolders (move its contents outside of it at the same hierarchical level + delete the nuisance folder), thus ending up with the files organised like this:
How can I achieve this, using a batch file, run from a command prompt inside Windows 7? I've tried a number of for statements with %F and %%F, but none worked. Grateful for any tips.
I believe the following will accomplish your goal if and only if all child folder and file names are unique. If there are duplicates, then all hell will break loose.
I have not tested, so backup and/or try the code on disposable data first.
You will have to modify the first PUSHD command to point to the root where all your "person n" folders reside.
#echo off
pushd "yourRootWherePersonFoldersReside"
for /d %%U in (*) do (
pushd "%%U"
for /f "eol=: delims=" %%A in ('dir /b /ad 2^>nul') do (
for /d %%B in ("%%A\*") do (
for /d %%C in ("%%B\*") do (
md "%%~nxC"
for /d %%D in ("%%C\*") do move "%%D\*" "%%~nxC" >nul 2>nul
)
)
rd /s /q "%%A"
)
popd
)
popd
The second FOR loop must be FOR /F instead of FOR /D because FOR /D has the potential to iterate folders that have been added after the loop has begun. FOR /F will cache the entire result of the DIR command before iteration begins, so the newly created folders are guaranteed not to interfere.

Batch scripting-cleanup script

i've engaged with work in batch script.these things i need to do
1.i want to find the folder say like "name" in particular directory
ex:
c:\test\name
c:\test\b\name
c:\test\n\c\name
2.in the name folder, need to delete all sub folders and files and all which is more than 90 days.
i have changed my question now please give me an idea...
#ECHO OFF
SETLOCAL
SET "targetdir=U:\destdir"
ECHO(DEL "%targetdir%\*?*"
FOR /f "delims=" %%a IN (
'dir /b /ad "%targetdir%\*" '
) DO (
ECHO(RD /S /Q "%%~a"
)
GOTO :EOF
You would need to change the setting of targetdir to suit your circumstances. It could of course be replaced by a literal if you wish.
The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(RD to RD to actually delete the directories.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
the del and for commands could be cascaded with a & if required. The for is spread across a nuber of lines for clarity.
You're missing an asterisk * so that the folder set expands to all subdirectories of the _delete folder. Also, the /D /R construct seems unnecessary on second thought, because RD /S does already take care of deleting directories recursively.
FOR /D %%A IN ( folder_you_want_to_clean\* ) DO IF EXIST "%%A" RD /S /Q "%%A"
But it won't delete the files in the folder_you_want_to_clean folder itself, though. Do that with the DEL command - the /Q option suppresses the confirmation prompt, just like with the RD command:
DEL /Q folder_you_want_to_clean\*
Concrete example: Suppose the folder you want to clean is C:\data\oldstuff. Then just do:
FOR /D %%A IN ( C:\data\oldstuff\* ) DO IF EXIST "%%A" RD /S /Q "%%A"
All directories contained in C:\data\oldstuff will be gone (and of course, any files in those directories). But any files in C:\data\oldstuff itself will still be there! So to delete those as well, do:
DEL /Q C:\data\oldstuff\*

Copy files from folder of lnk files back into folder

I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal

Make subfolder names from part of file name and copy files with Robocopy

Is it posible to copy and make directories automatically from file name substrings using Robocopy?
I mean i have files like these. LAJ00306130201004626.rc the first 8 chararacters are control number (LAJ00306=control number) this would be the name of the folder and the rest are the date and time (Date=130201) (time=004626).
LAJ00306130201004626.rc
LAJ00306130202004626.rc
LAJ00306130203004626.rc
LAJ00307130201004626.rc
LAJ00307130202004626.rc
and i would like to copy and create folders from the file name like under and copy the files mentioned before in the new folders.
LAJ00306
LAJ00307
I hope to be clear if necessary ask me for more information
try this, look at the output and remove the echos before MD and ROBOCOPY, if it looks good:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcefolder=."
SET "targetfolder=X:\data"
CD /d "%sourcefolder%"
FOR %%a IN (*.rc) DO (
SET "fname=%%a"
SET "folder=!fname:~0,8!"
SET "$!folder!=1"
)
FOR /f "delims=$=" %%a IN ('set "$"') DO (
ECHO MD "%targetfolder%\%%a" 2>nul
ECHO ROBOCOPY "%sourcefolder%" "%targetfolder%\%%a" "%%a*.rc"
)
Set sourcefolder and targetfolder for your folder tree.
Try this:
#echo off
pushd "c:\source folder"
setlocal enabledelayedexpansion
for %%a in (*.rc) do (
set "name=%%a"
robocopy "%cd%" "%%a" "D:\target directory\!name:~0,8!"
)
popd
Answers to your questions are:
pushd "drive:\path" makes the location the current working directory.
popd restores the last working directory
setlocal enabledelayedexpansion allows you to change and use variables within a loop, using the !variable! syntax.
If your 2000 files are in a single folder then it should work - but test it on some sample files first so that you can see how it will work.
#ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=c:\destdir"
FOR /f "tokens=1*delims=_" %%i IN (
'dir /b /a-d "%sourcedir%\*_*."'
) DO XCOPY /b "%sourcedir%\%%i_%%j" "%destdir%\%%i\"
GOTO :EOF
This should accomplish the task described. You'd need to set up the source and destination directories to suit, of course. Add >nul to the end of the XCOPY line to suppress 'copied' messages.

Resources