I have legal .strm files of different TV Shows in a folder named TV Shows. Each strm file of each episode is stored in different subfolders.
I would like to run a certain VBScript before these strm files are played.
Then I have a different folder named MOVIES. Again, I would like to run a VBScript before these strm files are played. But this VBScript is a different one.
How would I do that?
Platform is Windows.
Thanks!
If you're looking to open or do something to each file based on extension, An easy way to do this is to use an for loop with /R to search all sub-directories for an *.strm file.
#ECHO OFF
SET "LOC=C:\Folder"
CD %LOC%
FOR /R %%A IN (*.strm) DO (
Rem | Do something with each file.
Start cmd.exe /C "notepad.exe "%%A""
)
Echo No More Items Found!
pause
goto :EOF
%%A will return the file path.
Start cmd.exe /C "" will start a CMD command. (Replace with your code)
notepad.exe "" will open the file (Used as an example)
EDIT:
So you're looking to check if the file exists then if true run the script? Bellow should solve that.
#ECHO OFF
If exist (C:/Path/file.strm) (
Rem | File exists, run script
Echo Do something
)
GOTO :EOF
Related
I am trying to make a little shortcut for my daily work. I often have to copy some files from let's say C:\folder0\folder1\aaaa\ to C:\folder0\folder1\bbbb\.
I want to create a batch file shortcut in the send-to menu. So I would first select the files and then click on the new added shortcut to the batch file which should do the rest.
#echo off
:here
if '%1'=='' goto exit
"C:\Program Files (x86)\Notepad++\notepad++.exe" "%1"
echo %cd%
shift
goto here
:exit
I started with opening the files in Notepad++ and displaying the path.
But I need a function that stores the path from the given files and changes the folder a to folder b. Afterwards it would take the new path for the standard copy function.
xcopy /s C:\source D:\target
I hope I could properly explain what I try to achieve.
I found a solution. This is my code and it is working for me now as long as there are no spaces in the path (has someone an idea to fix that?)
#echo off
:here
if '%1'=='' goto exit
set strpath=%cd%
set strresult=%strpath:folder1=folder2%
#echo The original file '%1'
#echo New path %strresult%
coyp /b/v/y "%1" "%strresult%"
shift
goto here
:exit
pause
In my batch file, I first create a Lala file in folder: C:Lala-20160322-othercode and next day it creates the file in Lala-20160323-othercode and so on. In the same batch file, I want to use this created file. Now i do not know what to write after -filepath
Since Lala is in a differnt folder every time.
My batchfile is in folder above Lala-20160322-othercode. How can I do this?
The file itself is always called Lala, but on my computer there are many folder (every day another folder) with Lala.
I am new to batch files. Many thanks!!!!!!!!!!!!!!!!
Code:
#echo off
setlocal
call "C:\folder\NameofPythonProgramm" Pythonfunction -filepath ????? -pythonfunction
First I had %1 where the questionmarks are, but I want this dynamic file path
Save this as Test.bat, and run from its folder in open Cmd Prompt to access the file Lala* in the dir corresponding to your desired month. Let me know if any errors.
#echo off
setlocal enabledelayedexpansion
set "dir=c:\Lala-Months"
set "cur_month=August"
FOR /R "%dir%" %%G in (.) DO (set "mon=%%G"
if not '!mon:%cur_month%=!'=='!mon!' echo %%G )
exit /b
I'm publishing content to autogenerated folders and after the publishing has finished I want to copy files to that folder based on the foldersname using a batch
The autogenerated folders always have a language name, for example German, Dutch, French and English.
What I want my batch to do is that when the folder name is German it copies all the files from C:\Sourcefolder\DE\ to the new generated folder I'm running the batch from. I've tried to find something myself but my lack of knowledge results in this:
CHDIR /D %1
#ECHO OFF
SETLOCAL
SET "sourcedir=%cd%"
IF "%1"=="German" goto :German
:German
xcopy /Y "C:\Sourcefolder\DE\*.jpg" "%1"
GOTO :EOF
Is there anyone who can help me in the right direction?
Thanks in advance!
Here is the code for a batch file which might do what you want.
#echo off
rem Is this batch file called without any parameter?
if "%~1"=="" (
echo.
echo Run %~nx0 with language as first parameter.
echo.
echo Example: %~nx0 English
echo.
pause
goto :EOF
)
if /I "%~1"=="German" set "ShortName=DE" & goto CopyFiles
if /I "%~1"=="English" set "ShortName=EN" & goto CopyFiles
rem This batch file was called with a (language) string not listed above.
echo.
echo Error: "%~1" is not a supported language.
echo.
pause
goto :EOF
:CopyFiles
rem Copy all JPEG files of the specified language from source folder
rem to the specified language folder in current working directory.
xcopy /H /I /K /Q /R /Y "C:\SourceFolder\%ShortName%\*.jpg" "%~1"
set "ShortName="
Some notes additionally to the comments in the batch code:
%~1 is replaced by cmd.exe on execution of the batch file with the string of first parameter with removing double quotes if batch file was called for example with "English" instead of just English as first parameter.
If you want to know more about %~1 or %~nx0 (name of batch file with extension but without drive and path), open a command prompt window, run there call /? and read help output for this command.
/I option of command if makes the string comparison case-insensitive.
The ampersand in set "ShortName=DE" & goto CopyFiles concatenates the two commands set and goto on a single line which makes it possible here to specify 2 commands for each if without using parentheses. See Conditional Execution for details about this special operator.
For details on the switches used on command xcopy run in a command prompt window xcopy /? and read output help.
By the way: The batch files copies the JPEG files into specified language subfolder of current working directory. The current working directory can be the directory the batch file is stored, but can be also a different directory depending on how batch file was started and from which directory. If you want to make sure that the JPEG files are copied into specified language subfolder of batch file directory, you would need for the line with xcopy:
xcopy /H /I /K /Q /R /Y "C:\SourceFolder\%ShortName%\*.jpg" "%~dp0%~1"
I am really new to batch file coding and need your help.
I've these directories:
c:\rar\temp1\xy.jpg
c:\rar\temp1\sd.jpg
c:\rar\temp1\dd.jpg
c:\rar\temp2\ss.jpg
c:\rar\temp2\aa.jpg
c:\rar\temp2\sd.jpg
c:\rar\temp3\pp.jpg
c:\rar\temp3\ll.jpg
c:\rar\temp3\kk.jpg
And I want to compress them to this
c:\rar\temp1\temp1.rar
c:\rar\temp2\temp2.rar
c:\rar\temp3\temp3.rar
How could this be done using WinRAR?
This can be done also with WinRAR without using a batch file, not exactly as requested, but similar to what is wanted.
Start WinRAR and navigate to folder c:\rar\.
Select the folders temp1, temp2 and temp3 and click on button Add in the toolbar.
As archive name specify now the folder for the RAR archives, for example c:\rar\.
Switch to tab Files and check there the option Put each file to separate archive.
Click on button OK.
WinRAR creates now three RAR archives with the file names temp1.rar, temp2.rar and temp3.rar in folder c:\rar\ with each archive containing the appropriate folder with all files and subfolders.
The list of files to add can be changed also on tab Files by entering for example *.txt in Files to exclude to ignore text files in the three folders on creating the archives.
And finally it makes sense to enter *.jpg on tab Files in edit field below Files to store without compression as JPEG files usually contain already compressed data and therefore WinRAR cannot really compress the data of the files further.
Here is also a batch file solution to move the files in all non-hidden subfolders of c:\rar\ and their subfolders into an archive file with name of the subfolder created in each subfolder as requested.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RAREXE=Rar.exe"
if exist "%RAREXE%" goto CreateArchives
if exist "%ProgramFiles%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles%\WinRAR\Rar.exe" & goto CreateArchives
if exist "%ProgramFiles(x86)%\WinRAR\Rar.exe" set "RAREXE=%ProgramFiles(x86)%\WinRAR\Rar.exe" & goto CreateArchives
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "skip=2 tokens=1,2*" %%I in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe" /v Path 2^>nul') do (
if /I "%%I" == "Path" if exist "%%~K\Rar.exe" for %%L in ("%%~K\Rar.exe") do set "RAREXE=%%~fL" & goto CreateArchives
)
for /F "delims=" %%I in ('%SystemRoot%\System32\where.exe Rar.exe 2^>nul') do set "RAREXE=%%I" & goto CreateArchives
echo ERROR: Could not find Rar.exe!
echo/
echo Please define the variable RAREXE at top of the batch file
echo "%~f0"
echo with the full qualified file name of the executable Rar.exe.
echo/
pause
goto :EOF
:CreateArchives
set "Error="
for /D %%I in ("c:\rar\*") do (
echo Creating RAR archive for "%%I" ...
"%RAREXE%" m -# -cfg- -ep1 -idq -m3 -msgif;png;jpg;rar;zip -r -s- -tl -y -- "%%I\%%~nxI.rar" "%%I\"
if errorlevel 1 set "Error=1"
)
if defined Error echo/& pause
endlocal
The lines after set "RAREXE=Rar.exe" up to :CreateArchives can be omitted on definition of environment variable RAREXE with correct full qualified file name.
Please read the text file Rar.txt in the WinRAR program files folder for an explanation of RAR command m and the used switches. The question does not contain any information with which options the RAR archives should be created at all.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~f0 ... full name of batch file
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
reg /?
reg query /?
set /?
setlocal /?
where /?
See also single line with multiple commands using Windows batch file for an explanation of the operator &.
Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on the three FOR command lines to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded reg or where command line with using a separate command process started in background.
This script can work as well:
#echo off
for %%a in ("C:\rar\temp1" "C:\rar\temp2" "C:\rar\temp3") do (
pushd "%%~a"
"C:\Program Files\WinRAR\rar.exe" a -r temp.rar *
popd
)
In Python v3.x:
Tested on Python v3.7
Tested on Windows 10 x64
import os
# NOTE: Script is disabled by default, uncomment final line to run for real.
base_dir = "E:\target_dir"
# base_dir = os.getcwd() # Uncomment this to run on the directory the script is in.
# Stage 1: Get list of directories to compress. Top level only.
sub_dirs_raw = [os.path.join(base_dir, o) for o in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, o))]
# Stage 2: Allow us exclude directories we do not want (can omit this entire section if we wish).
dirs = []
for d in sub_dirs_raw:
if "legacy" in d or "legacy_old" in d:
continue # Skip unwanted directories
print(d)
dirs.append(d)
# Stage 3: Compress directories into .rar files.
for d in dirs:
os.chdir(d) # Change to target directory.
# Also adds 3% recovery record using "-rr3" switch.
cmd = f"\"C:\Program Files\\WinRAR\\rar.exe\" a -rr3 -r {d}.rar *"
print(cmd)
# Script is disabled by default, uncomment this next line to execute the command.
# os.system(cmd)
Notes:
Script will do nothing but print commands, unless the final line os.system(cmd) is uncommented by removing the leading # .
Run the script, it will print out the DOS commands that it will execute. When you are happy with the results, uncomment final line to run it for real.
Example: if there was a directory containing three folders mydir1, mydir2, mydir3, it would create three .rar files: mydir1.rar, mydir2.rar, mydir3.rar.
This demo code will skip directories with "legacy" and "legacy_old" in the name. You can update to add your own directories to skip.
To execute the script, install Python 3.x, paste the lines above into script.py, then run the DOS command python script.py from any directory. Set the target directory using the second line. Alternatively, run the script using PyCharm.
This should work it also checks if the files were compressed alright.
You may need to change this part "cd Program Files\WinRAR" depending on where winrar is installed.
#echo Off
Cd\
cd Program Files\WinRAR
rar a -r c:\rar\temp1\temp1.rar c:\rar\temp1\*.jpg c:\rar\temp1\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp2\temp2.rar c:\rar\temp2\*.jpg c:\rar\temp2\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
rar a -r c:\rar\temp3\temp3.rar c:\rar\temp3\*.jpg c:\rar\temp3\
if "%ERRORLEVEL%"=="0" ( Echo Files compressed
) Else Echo Failed
Pause
Below Script will compress each folder as a RAR file within the current directory with very useful info while compressing a large size of data.
#echo off
#for /D %%I in (".\*") do echo started at %date% %time% compressing... "%%I" && #"%ProgramFiles%\WinRAR\Rar.exe" a -cfg- -ep1 -inul -m5 -r -- "%%I.rar" "%%I\"
echo "Completed Successfully !!!"
pause
I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1