Open a folder using User input in CMD - windows

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%\"
)

Related

search the requested file in subfolders and copy to destination

I am trying to copy the listed files in file_list.txt to destination folder from source location, source folder has subfolders. my batch should be capable to search the file in source subfolders and copy to destination folder. same for copy all files with extension .exe. what is wrong with my code. I think, I have missed to search subfolders data. don't know what is the command. please help.
#Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
#color 0a
cls
set "dest=D:\destination"
set /p source=Select source path:
for /R %%f in (%source%/*.exe) do copy %%f "%dest%"
echo Copy requested files
for /f %%f in (file_list.txt) do (
for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%source%/%%f"') Do (
copy "%%F" "%dest%\%USERNAME%"
)
)
pause
ENDLOCAL
I cannot see anything obviously wrong with the code you've posted, although there are possibilities for errors. You have not provided sufficient information, about your current directory, the content of the text file, how the script is invoked or any debugging information.
The following version of your code, requires that you put your file listing text file into the variable definition on line five. I've assumed that the batch file is in the same location as the file listing, and therefore used %~dp0. If it is in the current directory instead, then replace that with %CD%\, and obviously the fully qualified absolute path if neither.
Next I use some validation to try to ensure that the source, destination, and user input exist. The input location will then become the current directory.
Your provided commands are then run, before the curent directory is returned to its original location.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Color 0A
Set "listdir=%~dp0filelist.txt"
Set "dest=D:\destination"
If Not Exist "%listdir%" GoTo :EOF
If Not Exist "%dest%\" GoTo :EOF
:Ask
ClS
Set "source="
Set /P "source=Select source path: "
If Not Defined source GoTo Ask
PushD "%source:"=%" 2> NUL || GoTo Ask
For /R %%G In (*.exe) Do Copy /Y "%%G" "%dest%"
Echo Copy requested files
For /F "UseBackQ EOL=? Delims=" %%G In ("%listdir%"
) Do For /F "Delims=" %%H In ('Dir "%%~nxG" /S /B /A:-D 2^> NUL'
) Do Copy /Y "%%H" "%dest%\%UserName%"
PopD
Pause
Feel free to try the code, and provide some proper debugging information if it still fails to work as intended.

Copy User Subfolders Using Robocopy

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'.

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

Windows Batch folder exist and folder not exist

Have a need where we need to delete zip files that exist under a specific folder at 100 days old. All other zip files in the other folders, we can delete that are 30 days old. This would be easy if the files were a specific name, unfortunately we can only do this by folder name.
In the end result we will be using one of these techniques with FORFILES as we don't have to do a lot of logic programming in script.
Searched and did find how to do this and works OK, but the processing of the script is very slow.. Pretty much at this point, everything in the "NOT" somefolder condition is what takes quite a while to complete. Have done this with VBScript and PowerShell, but really want to get back to Batch Scripting for this.
BAT script to search directory for folders that match an input name
and as dbenham admits, again this is very slow. Also, did not like the fact that it does not show all folders in the "not" condition for the ones it does not find
He also states, that if you want to do extensive file/folder searching redirecting to a output file, maybe the best solution
This does work, but does not show the "not found" or the folders it would list..
dir /s /a-d c:\windows\*system32* >nul && (echo found it) || (echo not found)
This works to find all folders with system32 on a drive
dir /b /ad /s "c:\system32"
This works to look for folders that DO HAVE system32 within the results which are pretty quick too
FOR /f "tokens=*" %%G IN ('dir /b /ad /s c:\ ^| findstr /I /C:"system32"') DO echo %%G
We want to show all folders that DO NOT HAVE system32 within and in testing and redirecting to result.txt file, it created a 11.9MB file and will take quite a while to complete especially on a whole drive
FOR /f "tokens=*" %%G IN ('dir /b /ad /s c:\ ^| findstr /I /V /C:"system32"') DO echo %%G
How do I search for the directory of a file given its name using a batch script?
Not quite sure about this one
for /r c:\ %%F in (system32) if exist "%%F" echo %%~dpF
Playing around we get
For /F %%A IN('dir /s /a-d "c:\windows\*system32*") do && (echo found it) || (echo not found)
Are there any other ideas or Suggestions out there?
This will search in c:\files and below, and delete zip files by the following criteria:
In the folder called Special folder it will delete them if they are 100 days or older
otherwise it will delete them if they are 30 days or older.
Any zip file in the folders below the Special folder will be deleted at 30 days and older.
Test it before use.
#echo off
for /r "c:\files" %%a in (*.zip) do (
for %%b in ("%%~dpa\.") do (
if /i "%%~nxb"=="Special folder" (
forfiles /p "%%~dpa." /m "%%~nxa" /d -100 /c "cmd /c del #path"
) else (
forfiles /p "%%~dpa." /m "%%~nxa" /d -30 /c "cmd /c del #path"
)
)
)
pause

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