Renaming Files With Batch - windows

I am new completely new to batch and have used Python, Java ect for about a year now. So as you can guess I dont know overly large amount.
I have been instructed by my employer to try and automate some tasks we do on a regular basis. This may sound like a stupid task that is easy to-do but it happens a lot and when talking to customers at the same time it get boring quick. The task: Renaming multiple folders... We do this when end users have forgotten their password or need a profile reset.
The code that I have at the moment is:
:again
#echo off
set /p answer= Rename a file/folder(Y/N)?
if /i "%answer:~,1%" EQU "Y" goto yes
if /i "%answer:~,1%" EQU "N" goto no
goto again
:yes
set /p dirOfFile = Directory of file:
set /p currentFolder = Name of folder to be re-named:
set /p newFolder = What will it be re-named to:
ren %dirOfFile%\%currentFolder% %newFolder%
:no
exit
The code is based off of a few sources, the Y/N and using variable from other StackOverflow posts and the ren %dirOfFile%\%currentFolder% %newFolder% from the cmd command of ren or rename.
When I use:
ren C:\Users\Zak\Desktop\TestFolder AlsoTest
It renames a folder on my desktop from TestFolder to AlsoTest. This is what I want the batch file todo, but once its working, on multiple folders.
Thanks for any/all help!

you don't have a parameter %dirOfFile%. But you have a parameter named %dirOfFile<space>%
Same with you other variables.
Write your setcommand without spaces:
set variable=value
for example:
set /p dirOfFile=Directory of file:

Simple You can rename any File with Batch
type the following in batch file
ren "oldfile.txt" "newfile.txt"

Related

Batch changing titles of mkv files in multiple directories

Ok, I'm total new at this...
Basically I'm using a tool call mkvpropedit to edit the title of my .mkv files
My aim is to create a batch the goes through all sub directories and replace the mkv file titles with their file name.
I have made the following progress...
for %%A in (*.mkv) do "C:\mkvpropedit.exe" "%%A" --edit info --set title="%%A"
Issue with [1]: It works fine but does not affect all sub directories and I would have to use the batch in all the sub directories one by one which will be time consuming.
for /R "C:\whatever" %%I in (*mkv) do "C:\whatever\mkvpropedit.exe" "%%I" --edit info --set title="%%I"
Issue here, It affects all sub directories but my .mkv file titles end up with the entire directory pathway instead of the file name.
Could anyone help me here? Thanks a lot in advance.
BTW if anyone know how to set a long directory pathway into a short form to be use repeated throughout the script (eg. "C:\whatever\whatever...\mkvpropeditexe into mkvpropedit", that would be helpful.
Whether you use %%~nI or %%~nxI (as suggested by Gerhard Barnard) depends on how you want the title: "name" only or "name.extension".
for how to set a long directory pathway into a short form to be use repeated throughout the script; set a variable with the full path\name and use the variable:
set "mkv=C:\whatever\mkvpropedit.exe"
for /R "C:\whatever" %%I in (*.mkv) do "%mkv%" "%%I" --edit info --set title="%%~nI"
Using the help from this thread, here's a little more elaborate batch script I developed:
rem This Bat file will take MKV filenames and apply them to MKV info titles
#echo off
rem Modify next line to path where mkvpropedit.exe !!!!!!!!!
cd "C:\Program Files\MKVToolNix"
set /A errors1=0
rem Modify next line to path where MKV files are. This will also modify MKV's in subdirectories. !!!!!!!!!
for /R "X:\Move" %%X in (*.mkv) DO CALL :loopbody %%X
echo.
echo.
echo Total Errors = %errors1%
echo.
pause
GOTO :EOF
:loopbody
set title0=%*
set "title1=%title0:.mkv=%"
set "title2=%title1:\=" & set "title2=%"
rem The following two lines are to remove additional info I use in the filenames.
set "title3=%title2: 1080p=%"
set "title4=%title3: 720p=%"
set command1=mkvpropedit "%title0%" --edit info --set "title=%title4%"
for /f "delims=" %%a in ('%command1%') do #set response1=%%a
echo %title2%
echo %response1%
echo.
echo.
if /i "%response1:~0,5%"=="Error" (set /A errors1=%errors1% + 1)
GOTO :EOF

Copy a file with batch in a user selected directory

i'm looking for a .bat file that, on opening, ask the user what folder he want to select, then, the batch copy a file (python script) in this folder and execute it
for now i use :
xcopy c:/pythonfiletocopy d:/destinationpath
but i dont find a way to make the user choose the destination folder
any idea ?
thank you
You can request user input using the Set command together with its /P option.
Here's an example which should accept typed or pasted input and should also accept drag and drop for directories not containing spaces:
#Echo Off
Set "_in=" & Set /P "_in=Please provide a directory for the file: "
If Defined _in If Exist "%_in%\" Echo XCopy "C:\pythonfiletocopy" "%_in%"
Pause
I have added an Echo on line 3, if you're happy with the output you can remove that.
I know you said you didn't need one but you could JScript launch a GUI directory browser for input.
Here's an example which may do that:
0</* :
#Echo Off
For /F "Delims=" %%A In ('CScript //E:JScript //NoLogo "%~f0" 2^>Nul'
) Do Echo XCopy "C:\pythonfiletocopy" "%%A"
Pause
Exit /B */0;
var Folder=new ActiveXObject('Shell.Application').BrowseForFolder(0,'',1,'::{20D04FE0-3AEA-1069-A2D8-08002B30309D}');
try{new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(Folder.Self.Path)};catch(e){};close();
I have added an Echo on line 4, if you're happy with the output you can remove it and line 5.

How to list down hidden files using a .BAT File?

For my computer project, my teacher requires us to make a bat file that lists down hidden files. The whole class doesn't know how though. Here is my code so far:
#echo off
set /p UserInput=What directory would you like?
cd %UserInput%
dir /S /aH
Pause
My teacher says I'm almost done, I'm just missing 2 or 3 characters
after "dir /S /aH". Would anyone know what these missing characters are?
Hi guys, I'm sorry for the late reply. We're having so many projects before the semester ends. :( When I run the bat file, it shows the following.. Here's the link: imgur.com/W4Wm4vP
Whilst you've already received responses that are sufficient to your initial request, I've added one just to show that you should really try to perform some sort of verification of user input before proceeding with your listing:
#ECHO OFF
:LOOP
CLS
SET /P "UserInput=What directory would you like? "
IF "%UserInput%"=="" GOTO LOOP
IF /I NOT "%CD%"=="%UserInput%" (PUSHD "%UserInput%" 2>NUL||GOTO LOOP)
DIR /B /S /AH-D
TIMEOUT /T -1 /NOBREAK
I trust this will give you a few more things to research and learn.
Edit
As an after thought, if the end user is inputting only a directory name and not a full path then you may need to replace line six with:
FOR %%A IN ("%CD%") DO IF /I NOT "%%~nxA"=="%UserInput%" (
PUSHD "%UserInput%" 2>NUL||GOTO LOOP)

How to get attributes of a file using batch file

I am trying to make a batch file to delete malicious files from pendrive. I know that these malicious files uses hidden,read only and system attributes mainly to hide itself from users. Currently i am deleting these files using cmd by removing malicious files attributes then deleting it. Now I am thinking to make a small batch file which can be used to remove these files just by entering the drive letter.
I have found this code in a website to find attributes of a file. But after entering the name of the file the batch file just exits without showing any results.
#echo off
setlocal enabledelayedexpansion
color 0a
title Find Attributes in Files
:start
set /p atname=Name of the file:
if not exist %atname% (
cls
echo No file of that name exists!
echo.
echo Press any key to go back
pause>nul
goto start
)
for /f %%i in (%atname%) do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
echo Press any key to go back
pause>nul
goto start
can you tell me why this batch file is exiting without showing any results. Or can you give any better batch script for getting attributes of a file.
EDIT
I was able to work the above code only for a single file. As my purpose of my batch file is to remove malicious files by entering the drive letter. How can i use it to find what kind of attributes files are using in a particular drive.
For example:
In cmd we can use this command to find the file attributes of a given drive
attrib *.*
Advance thanks for your help
I tried the bat file (without inspecting the details) and it seems to work fine for me. What I noticed is that it closes instantly if you don't enclose file path with quotation marks - e.g. "file". Example:
Name of the file: path\file.txt // this will close immediately
Name of the file: "path\file.txt" // now it will stay open and display the result
This hopefully solves your problem.
As far as your question in EDIT is concerned, a simple option is to iterate a list of files and execute the batch on each one.
batch1.bat: (%1 refers to the first command-line parameter)
#echo off
setlocal enabledelayedexpansion
echo %1
set atname=%1
for %%i in ("%atname%") do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
Next, generate a list of all files within a given path (say 'folder' including all subfolders):
dir /s /b folder > ListOfFiles.txt
main.bat (read ListOfFiles.txt line-by-line and pass each line to batch1.bat as a command line parameter):
#echo off
for /f "tokens=*" %%l in (ListOfFiles.txt) do (batch1.bat %%l)
Then, from cmd:
main.bat >> output.txt
The last step generates an output file with complete results. Granted, this can be done in a more polished (and probably shorter) way, but that's one obvious direction you could take.
You're using a for /f loop here, which isn't necessary (and may yield undesired results if the filename contains spaces). Change this:
for /f %%i in (%atname%) do set attribs=%%~ai
into this:
for %%i in ("%atname%") do set attribs=%%~ai
This is dangerous code - but it'll delete read only, hidden and system files.
It should fail to run on c: drive but I haven't tested it. Note that some Windows installs are on drives other than c:
#echo off
echo "%cd%"|find /i "c:\" >nul || (
del *.??? /ar /s /f
del *.??? /ah /s
del *.??? /as /s
)

Batch - display folder names as options

I'm trying to create a small batch file which reads a folder (path is set as a variable in the file). It should display the names of all sub-folders as choices for the user and when the user chooses one that folder name should be saved in a variable for later use. The idea is that I have alot of branches I'm working on and in all of them there is a little jar file I want to run with this batch. So the batch present me a list of all branches in the folder and when I pick on it will start the jar file located in that branch folder.
EXAMPLE:
C:\code
contains
C:\code\branch1
C:\code\branch2
C:\code\branch3
Then I want the batch to present the following menu to the user:
1. branch1
2. branch2
3. branch3
When the user has chosen the folder name (f.ex. branch2) is saved in a variable for later use.
I've tried alot of googling, but nothing helpful came up. Sofar I've managed to read the sub-folders' names, but I dont know where to go from here.. can anyone point me in the right direction?
We first need delayed expansion
setlocal enabledelayedexpansion
Then need a list of all subfolders (assuming %dir% being set to the directory you want subfolders of):
set Index=1
for /d %%D in (%dir%\*) do (
set "Subfolders[!Index!]=%%D"
set /a Index+=1
)
set /a UBound=Index-1
Then you can present a choice (I added a little input validation, but it's not enough):
for /l %%i in (1,1,%UBound%) do echo %%i. !Subfolders[%%i]!
:choiceloop
set /p Choice=Your choice:
if "%Choice%"=="" goto chioceloop
if %Choice% LSS 1 goto choiceloop
if %Choice% GTR %UBound% goto choiceloop
Then you can set a variable with the subfolder the user chose:
set Subfolder=!Subfolders[%Choice%]!

Resources