Using a batch file, I am trying to get the last foldername in a path.
The batch file gets the current working directory, goes up one level and uses that foldername. The problem is that if the foldername has spaces like "My Project", then the below code will return just "Project".
#echo off
cls
:: get pathnames
set ProjectRoot=%~dp0..\
set ProjectRootLast=%ProjectRoot:~0, -1%
for %%f in (%ProjectRootLast%) do (
set ProjectName=%%~nxf
)
echo %ProjectRoot%
echo %ProjectName%
pause
aschipfl is right, you should use:
#echo off
cls
:: get pathnames
set "ProjectRoot=%~dp0..\"
set "ProjectRootLast=%ProjectRoot:~0,-1%"
for %%f in ("%ProjectRootLast%") do (
set "ProjectName=%%~nxf"
)
echo %ProjectRoot%
echo %ProjectName%
pause
But you could do this much more efficiently using
for %%* in (.) do echo %%~nx*
to get the name of the current directory and
for %%* in (./..) do echo %%~nx*
To get the name of the directory above that
Related
For a given directory, How to find .java file in current and sub-directory and verify if the file containing "com.demo" then replace with "com.practice" in Windows bat command?
Assumptions:
OS: Windows 10
After doing lot of google I came to below command but it neither throwing error nor doing anything.
setlocal enabledelayedexpansion
for %%f in ("<my direcoty path>*.java") do (
type %%f | repl "com.demo" "com.practice" >%%f.new
move %%f.new %%f
set /p val=<%%f
echo %val%
echo "fullname: %%f"
echo "name: %%~nf"
echo "contents: !val!"
)
pause
I have a game that I play and mod a lot, and a lot of the files in the game have file extensions that are in all caps, which bothers me quite a bit. I'm trying to change them all to be lowercase, but there are numerous folders in the game files, so I'm having to be very repetitive. Right now, I'm working with this:
cd\program files (x86)\Activision\X-Men Legends 2\Actors
start ren *.IGB *.igb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\
start ren *.XMLB *.xmlb
cd\program files (x86)\Activision\X-Men Legends 2\Conversations\act0\tutorial\tutorial1
start ren *.XMLB *.xmlb
and so on for each and every folder in the game files. I have a very long .bat file where I just have line after line of this but with a different destination folder. Is there a way to streamline this process so I don't have to manually type out each folder name? Also, is there a line that I could add at the beginning to automatically run as an administrator, so I don't have to make sure to run the .bat file as an administrator each time?
I'm not looking for anything complicated, and I'm very inexperienced with coding other than the small amount of stuff I've been able to search up.
Instead of doing it for each folder, use a for /R loop which loops through all subfolders. I would suggest the following code:
#echo off
:prompt
set /p "extensions=What are the up-case extensions you want to convert to lower-case?: "
if not defined extensions (cls & goto:prompt) else (goto:loop)
:loop
for %%A IN (%extensions%) do (
for /R "custom_folder" %%B IN (*.%%A) do (
ren "%%~fB" "%%~nB.%%A"
)
)
Take a look on this on how to run this batch file as admin. Create another batch file and add the code specified in the accepted answer.
Note: As Stephan pointed out in the comments, you can use %ProgramFiles(x86)% environment variable which is the same thing.
#echo off
setlocal
rem Check if admin.
2>nul >nul net session || goto :runasadmin
rem Start in script directory.
pushd "%~dp0" || (
>&2 echo Failed to change directory to "%~dp0".
pause
exit /b 1
)
rem Ask for directory to change to, else use the script directory if undefined.
set "dirpath=%~dp0"
set /p "dirpath=Dir path: "
rem Expand any environmental variables used in input.
call set "dirpath=%dirpath%"
rem Start in the input directory.
pushd "%dirpath%" || (
>&2 echo Failed to change directory to "%dirpath%".
pause
exit /b 1
)
rem Ask for file extensions.
echo File extensions to convert to lowercase, input lowercase.
echo i.e. doc txt
set "fileext="
set /p "fileext=File extension(s): "
if not defined fileext (
>&2 echo Failed to input file extension.
pause
exit /b 1
)
rem Display current settings.
echo dirpath: %dirpath%
echo fileext: %fileext%
pause
rem Do recursive renaming.
for %%A in (%fileext%) do for /r %%B in (*.%%A) do ren "%%~B" "%%~nB.%%A"
rem Restore to previous working directory.
popd
echo Task done.
pause
exit /b 0
:runasadmin
rem Make temporary random directory.
set "tmpdir=%temp%\%random%"
mkdir "%tmpdir%" || (
>&2 echo Failed to create temporary directory.
exit /b 1
)
rem Make VBS file to run cmd.exe as admin.
(
echo Set UAC = CreateObject^("Shell.Application"^)
echo UAC.ShellExecute "cmd.exe", "/c ""%~f0""", "", "runas", 1
) > "%tmpdir%\getadmin.vbs"
"%tmpdir%\getadmin.vbs"
rem Remove temporary random directory.
rd /s /q "%tmpdir%"
exit /b
This script is expected to start from double-click.
It will restart the script as admin if not already admin.
It will prompt to get information such as directory to change to and get file extensions i.e. doc txt (not *.doc *.txt). If you enter i.e. %cd% as the directory input, it will be expanded.
I have got a text file where in I store 'file names' along with its path
Example
Modules\App\a.txt
Modules\Engine\b.txt
I am able to get 'filename' using #echo %%~nxA.
However, when I try #echo %%~pA it prints my current windows path and file path
Example
When I execute the script from D:\Temp it prepends \Temp
\Temp\Modules\App\a.txt
\Temp\Modules\Engine\b.txt
However, output should be 'Modules\App\a.txt' and NOT '\Temp\Modules\App\a.txt'
In short I am looking out for an windows equivalent of dirname
You seem to want relative paths. for isn't able to provide that, but you can work around it - just remove the current folder path:
#echo off
setlocal enabledelayedexpansion
(echo Modules\App\a.txt
echo Modules\Engine\b.txt)>file.txt
for /f "delims=" %%A in (file.txt) do (
set "tmp=%%~fA"
REM set "tmp=%%~dpA"
echo !tmp:%cd%\=!
)
Result:
Modules\App\a.txt
Modules\Engine\b.txt
I'm trying to use the name of a directory, but I can't get the name in a variable. Here's my current script:
MKDIR "D:\Documents\testbatch\temp"
SET TEMPDossier="D:\Documents\testbatch\temp"
:Check
CD /D D:\Documents\testbatch
FOR /D %%x in (*_C_* ) do (
CALL :fct_log_Texte INFO " dossier disponible %%x"
setlocal enabledelayedexpansion
SET VarDossier= %%x
ECHO !VarDossier!
SET rename=!VarDossier!
echo %rename%
:: Quote the variable and remove the !
Set rename="%rename:!=%"
I can't understand your code but if you want to get the directory name from full path of it, you can use call command with variable like this:
#echo off
set fullpath="D:\Documents\testbatch\temp temp"
call :show %fullpath%
pause
goto :eof
:show
echo %~n1
If you run above batch file, it will print temp temp on the screen.No quotes, no !. This is just example. I think you can modify for yourself. I hope it is useful for you.
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
)