I am having trouble with creating a batch file which then can be mapped to a keyboard shortcut, The basic's of the script is it asks for the season, episode and shot and finds it via that input in the directory however I having trouble with the fact that some folders have specific names at the end and I cant understand how to go about creating wild cards.
#echo off
set /p Series=Series:
set /p Episode=Episode:
set Series=00%Series%
set Episode=00000%Episode%
set Episode=%Episode:~-3%
rem open up review
set Path="P:\projectName\06_review\series_%Series%\sr%Series%_ep_%Episode%"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n
:n
start %NEW%
So far this is doing well grabbing the latest file but once it needs to grab a folder that has more characters after \sr%Series%_ep_%Episode% it fails, but what is the best way to create wild cards for any possible string after the matching input?
Related
I have an existing script that loops through a list of folders, and deletes a file from each folder found. I want the script to skip specific folders in the list based on name.
Here is the existing script:
for /d %%F in (c:\users\*) do del "%%F\AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations\f01b4d95cf55d32a.automaticDestinations-ms" /s /q
In this case, I would want it to skip the "admin" folder found "c:\users".
The code below is designed for Windows 10, as your task is for resetting the Quick Access Pinned Folders in its File Explorer.
The code already ignores the 'Special' PC accounts, that includes, but may not be limited to, Administrator, DefaultAccount, Guest, and WDAGUtilityAccountdefault.
If you have other named accounts you want to exclude from this task, you could insert them at line 7 between ' and " using the following syntax: And Name='firstName' Or Name='anotherName' Or Name='additionalName'
For example:
"LocalAccount='TRUE' And Name='admin' Or Name='john' Or Name='jane'"
Nothing else should be modified, added or removed for testing purposes.
#Echo Off
SetLocal EnableExtensions
Set "SubPath=AppData\Roaming\Microsoft\Windows\Recent\AutomaticDestinations"
Set "FileName=f01b4d95cf55d32a.automaticDestinations-ms"
For /F Tokens^=6^ Delims^=^" %%G In ('
%SystemRoot%\System32\wbem\WMIC.exe UserAccount Where
"LocalAccount='TRUE'"
Get SID /Format:MOF 2^>NUL') Do For /F Tokens^=6^ Delims^=^" %%H In ('
%SystemRoot%\System32\wbem\WMIC.exe Path Win32_UserProfile Where
"SID='%%G' And Special!='TRUE' And LocalPath Is Not Null" Get LocalPath
/Format:MOF 2^>NUL') Do Del /A /F "%%H\%SubPath%\%FileName%" 2>NUL
The code may look complex, but its benefits are that it ignores 'Special' accounts, and it does not limit their local profile paths to C:\Users. Not every user has to have their profile directory in the default location, so this determines that location without making such assumptions.
I have also modified your Del command options because the file you are wanting to delete is not located one or more times within the tree, negating /S, and as you are not using a global wildcard, you do not need the /Q option either. I have included the /A and /F options just in case the file has problematic attributes, just to improve the chances of successful deletion.
Please be aware that if your task involves removing files from another user's profile tree, then the invoker of the task must have the required permissions to do so.
I've a fairly rudimentary knowledge of batch files and usually manage to get by but have hit a problem which I can't solve.
The batch files are run on Windows 7 Ultimate and Windows 10 Professional systems and are usually invoked by a Scheduler program, although sometimes I just click the relevant desktop icon. Essentially, the role of the batch files is to download specific files (TV programmes) which are listed in an external text file, in my case, located in my Dropbox account. For each item in the text file (TV.txt) there are two lines, one naming the file, the other listing its ID:
name1
ID1
name2
ID2
name3
ID3
The batch files successively work through the items listed in the text file, one file works on the "IDs", the second on the "names".
The "IDs" file (tv-pid.cmd) consists of the following:
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --pid %%a
The "names" file (tv-nopid.cmd) consists of the following:
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --get %%a
Each batch file works well on its own, the problem is when I try to combine the two into a single batch file.
If I create a "combined" batch file (tv.cmd):
call tv-pid.cmd
call tv-nopid.cmd
the first "call" is executed but the batch operation terminates before calling the second file.
Equally if I create a "combined" batch file (not using "call" commands)
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --pid %%a
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
for /f "delims=" %%a in ('type %$textFile%') do get_iplayer --get %%a
the same happens, the download activity on line 2 is executed after which the batch operation terminates.
Personally I would prefer a solution based on the "call" commands, but I don't mind.
set $textFile="D:\Dropbox\Get_iPlayer\0-TVdl\tv.txt"
set "idnames="
for /f "delims=" %%a in ('type %$textFile%') do (
if defined id_names (
set "id_names="
call get_iplayer --pid %%a
) else (
set "id_names=Y"
call get_iplayer --get %%a
)
This may work. I've no idea what get_iplayer is or does.
The idea here is that the line-content alternates, so toggling the variable id_names between set-to-a-value and set-to-no-value (=cleared) allows us to execute get_iplayer with the correct options.
Note that your code would execute get_iplayer with the option pid or get for each line of the input file - which may be causing the problem.
My conundrum is related to the q/a thread at the following link: How to append date to directory path in xcopy
I'm new to this forum, and I had the same kind of question, and I'm using Windows 10, so I used the answer given in that thread by foxidrive about how to use WMIC for this, and it works fabulously, except for one issue that I've not yet figured out...
I modified the script that foxidrive provided, as follows:
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,2%
set YY=%dt:~2,2%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
for /f "tokens=%dow%" %%a in ("Su Mo Tu We Th Fr Sa") do set day=%%a
set stamp=%YY%%MM%%DD%%HH%%Min%%day%
REM echo Today is %day%.
md "%stamp%MoreDirName"
xcopy %source% /E /y .\"%stamp%MoreDirName"
When I run the batch file from cmd.exe, I get the desired result, namely, a directory is created with the date format the way I want it, and the date and time stamp that I want includes the name of the day of the week. However, when I double-click on the batch file in windows explorer, the folder is created and folders/files are copied, but the name of the day of the week does not appear in the new folder name. I am confused by this behavior and I would like to know how to override it, please.
I would have researched the issue more, but I am not sure what to search for other than ``different behaviors of WMIC in command line and windows'', and such a search yielded no helpful results. But since my efforts were based specifically upon the referenced stack exchange q/a thread, it seems to me that this is an appropriate place to document this strange behavior and get explanations if possible, which might help me, and others later, to compose better script.
I am confused by this behavior and I would like to know how to override it
There is no problem with wmic. The Issue is with your batch file, which contains two undefined variables).
for /f "tokens=%dow%" %%a in ("Su Mo Tu We Th Fr Sa") do set day=%%a
You don't set dow anywhere in your batch file.
xcopy %source% /E /y .\"%stamp%MoreDirName"
You also don't set source anywhere in your batch file.
What probably happened:
You have dow and source hanging about in your cmd environment from another batch file you have run that does not include the setlocal command (which prevents variables leaking into the parent cmd shell).
That means:
The batch file run from a cmd shell will work if dow and source are set in that instance of cmd.
The batch run from explorer won't work because it start a new instance of the cmd shell and dow and source are undefined.
Corrected batch file:
Here is a modified version of your batch file that will work when run from a cmd shell or from explorer, and correctly sets up Day of the Week.
rem #echo off
setlocal
set source=SomeSourceValue
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1-6" %%g in (`wmic Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"`) do (
set _day=00%%g
set _hours=00%%h
set _minutes=00%%i
set _month=00%%j
set _seconds=00%%k
set _year=%%l
)
rem pad with leading zeros
set _month=%_month:~-2%
set _day=%_day:~-2%
set _hh=%_hours:~-2%
set _mm=%_minutes:~-2%
set _ss=%_seconds:~-2%
rem get day of the week
for /f %%k in ('powershell ^(get-date^).DayOfWeek') do (
set _dow=%%k
)
set _stamp=%_year%%_month%%_day%%_hh%%_mm%%_dow:~0,2%
md "%_stamp%MoreDirName"
xcopy %source% /E /y .\"%_stamp%MoreDirName"
endlocal
Notes:
The batch file uses a more elegant way to retrieve the timestamp components from wmic.
Modify the above as appropriate to set source correctly.
Credits:
Thanks to Danny Beckett for this answer which provided the PowerShell weekday trick.
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
for /f - Loop command against the results of another command.
getdate - Display the date and time independent of OS Locale, Language or the users chosen date format (Control Panel/Regional).
setlocal - Set options to control the visibility of environment variables in a batch file.
variables - Extract part of a variable (substring).
wmic - Windows Management Instrumentation Command.
I have a Master folder which have multiple sub folders. All the subfolders have lot images with different extensions (jpg,tif and png). The total number of images in all the subfolder are around 90000 images.
The thing is, I need to search some 500 images in Master folder and its subfolders and move the images to specified folder.
I tried a below batch script to use the text file to search through a Master folder and all subfolders and move all the files from the list and paste them into a Specified single folder.
The text file which contains file names without extension.
But my batch script is not working. It didnt throw me any error.. but nothing happens when I run it.
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f %%X in (%FIILELIST%) do call :MOVE_FILES "%%X"
goto :eof
:MOVE_FILES
for /r %FILESPATH% %%I in (%~1) do echo move /qvs "%%I" "%DESTPATH%%%~pnxI"
I am very new to batch script and in learning stage. Kindly help me in this. Im very much thankful if anyone provide the correct batch script to do this.
Can you try this?
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f "delims=" %%x in (%FIILELIST%) do (forfiles /p %FILESPATH% /s /m %%x.* /c "cmd /c move /y #path %DESTPATH%\#file" 2>>failed_temp.txt)
for /f "tokens=5 " %i in (failed_temp.txt) do (echo.%~i)>>failed_list.txt
del failed_temp.txt
Cheers, G
#gbabu's answer was super helpful for me.
I needed to edit it to handle file names that were absolute (full) instead of relative. And I needed to handle that they contained spaces.
Unfortunately I couldn't figure out how to log the errors like #gbabu did.
#echo off
REM See https://stackoverflow.com/a/25325529/470749
REM See https://stackoverflow.com/a/163873/470749
REM See https://stackoverflow.com/a/155950/470749
set FILELIST="K:\F\Users\my_user\Documents\My Music\JUKEBOX\5.m3u_list.txt"
set DESTPATH=C:\temp\cdrw
for /f "usebackq tokens=*" %%x in (%FILELIST%) do (copy "%%x" %DESTPATH%)
pause
These articles helped me:
https://stackoverflow.com/a/25325529/470749
https://stackoverflow.com/a/163873/470749
https://stackoverflow.com/a/155950/470749
I have ZERO experience with this. Any and all help would be greatly appreciated.
Right now I have it so I can see the list of folders I am creating and edit which ones I want to create. I want to be able to create Fund Family folders and in each of those Fund Family folders have various entities and then within each entity create folders. I think this requires referencing many different outputlists. So like at the top the fundfamily list is fine...i think i need serval outpistlits for each funfamily that will contain the entities so entitylist01...entitylist02... if that makes sense
There are 32 fund families with around 600 entities in total. The entity list changes for each fund family.
this what I have so far...
mkdir C:\2013\"2 - November Projections"
pause
dir Z:\2013\"2 - November Projections"\"*" /b /a:d >fundfamily.txt
start C:\Windows\System32\notepad.exe "fundfamily.txt"
pause
for /f "tokens=*" %%a in (fundfamily.txt) do mkdir C:\2013\"2 - November Projections"\"%%~a"
pause
for /f "tokens=*" %%a in (fundfamily.txt) do dir Z:\2013\"2 - November Projections"\"%%~a"\"*" /b /a:d >entitylist.txt
start C:\Windows\System32\notepad.exe "entitylist.txt"
pause
for /f "tokens=*" %%a in (entitylist.txt) do mkdir C:\2013\"2 - November Projections"\"*"\"%%~a"\2013\"November Estimates"
pause
#echo off
setlocal
SET "sourcedrive=Z"
SET "destdrive=C"
SET /a year=2013
SET "projections=2 - November Projections"
SET "Estimates=November Estimates"
DIR /b /ad "%sourcedrive%:\%year%\%projections%" >fundfamily.txt
start /wait "Fund Family" C:\Windows\System32\notepad.exe "fundfamily.txt"
FOR /f "delims=" %%a IN (fundfamily.txt) DO (
ECHO MD "%destdrive%:\%year%\%projections%\%%~a"
DIR /b /ad "%sourcedrive%:\%year%\%projections%\%%~a" >entitylist.txt
start /wait "%%~a entities" C:\Windows\System32\notepad.exe "entitylist.txt"
FOR /f "delims=" %%b IN (entitylist.txt) DO ECHO MD "%destdrive%:\%year%\%projections%\%%~a\%%~b\%year%\%estimates%"
)
It's incredibly difficult to solve a problem stated as ""my code doesn't do what I want it to do" without an indication of sample data or desired outcome.
For instance, you could provide a partial listing such as
fundfamily.txt
Jones
Brown
de la Vedova
Smith
entitylist.txt
Stock - Confusion Industries
Sweatshop Industrial Inc.
Royalties - Useless Invention
123, Slum St., Gloomsville
and a note like the entity list changes for each fundfamily - or renaims the same, or whatever. You can Edit this data into your question - 'tis what the Edit link under the tags is used for.
As for the above code - I've assumed that the structure is as I have indicated. I would suggest that it would be quite an onerous task if you have hundreds of fundfamily entries, but we have no further data to go on...so it's "air" code - untried for absence of available data.
Note that the first quoted string in the start/wait line becomes the window title. If you don't want a window title, use an empty string ""
I've left C:\Windows\System32\notepad.exe in place, but it's quite likely that simply notepad would suffice, or even that it could be omitted entirely. The /wait is the magic - this waits until the executable is finished before the batch proceeds to the next step.
Also note that the MD commands (MD and mkdir are synonyms) are merely echoed - so that you may test without actually changing anything. You'd need to remove the echo keyword before the md to actually create the directories.