Batch: Set one directory up path in variable - batch-file

Hi i am mapping a shell script code to batch script so i came across a statement in shell script like this
BASEDIR=`cd ../;pwd`
But have no idea how to do that. I know that %CD% will give me current directory but i want to set one directory up path in variable . Please help.

A bit more complex as in shell code.
FOR /F "delims=" %%A in ("%CD%\..") do set "basedir=%%~fA"
Edit related to your comment:
To replace \ with \\ just add the line
set "basedir=%basedir:\=\\%"

Another way...
pushd ..
set "BaseDir=%CD%"
popd

for /f "delims=" %%a in ("%cd%") do set "upper_dir=%%~dpa"

Consider:
#echo off
set "dir=C:\Windows\System32"
call:up %dir%
:up
echo/%~dp1

Related

Batch File: "Environment Variable Not Defined" on a path

I have the following in a batch file
for /f "delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set %%a
net use J: https://csv/dav %p% /user:%u% /persistent:yes
I get an error:
Environment variable C:\Program Files (x86)\Wowza\creds.txt not defined
What do I need to resolve this?
Secondly, it works for all colleagues apart from one. Same laptop make, model and build. I used my details and it failed on his but worked on mine.
What fails is that it asks for the credentials to map the drive instead of taking them from the file
creds.txt
u:JoeBloggs
p:Password1234
Any idea?
Thanks
the reason for your errormessage is, your for /f loop doesn't evaluate the contents of the file. It takes a quoted string as string not as filename. Usebackq changes that behaviour.
You have another failure in your script: With your code, set %%a translates to set u:JoeBloggs, which is invalid syntax. Correct syntax requrires set u=Joebloggs. Therefore you have to split the line in a part before the colon and a part after the colon and build your set command accordingly (just set %%a would work, when the contents of the file would look like u=JoeBloggs)
Change your for loop to:
for /f "usebackq tokens=1,* delims=:" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%a=%%b"
I was going to post a similar answer to #stephan but he beat me to it. If however you have the option to change your creds.txt file to the below:
u=JoeBloggs
p=Password1234
You can shorten the for loop a bit to this:
for /f "usebackq delims=" %%a in ("C:\Program Files (x86)\Wowza\creds.txt") do set "%%~a"
which effectively just does this:
set "u=JoeBloggs"
set "p=Password1234"

Batch File - Get filename from directory and save as variable

I am trying to read in a directory and get the filename from that directory.
I then want to save the filename as a variable and echo that filename out.
Here is the code I am using:
for /F %%a in ('dir C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\ *.csv') do set FileName=%%a
echo %FileName%
I am getting the following when command prompt runs:
"File not found
Directory"
Does anyone know how to resolve this or where I'm going wrong?
Thanks
Safer way of doing the same:
#echo off
setlocal
set "yourDir=C:\Users\username\Documents\Training\Pentaho\Outputs\BatchFileOutput\"
set "yourExt=*.csv"
pushd %yourDir%
for %%a in (%yourExt%) do echo %%a
popd
endlocal
Sets both: Your directory and the extension you are searching for, Changes the directory to the one previously setted possibly including a /drive change and then runs a loop over all files matching your extension and echo them out. To save only the last one you can use:
...do set fileName=%%a
echo %FileName%
Or to use them all within the loop you can use:
#echo off
Setlocal EnableDelayedExpansion
REM Other things done here
do (
REM Do stuff with %%a here
Set filename=%%a
echo !filename!
echo !filename:~0,6!
echo !filename:a=b!
)
If you just want to echo them, you can just go for echo %%a. If you want to do other things like string-substitution or substrings as described in the comments you need DelayedExpansion as shown above. There are a lot of questions on SO as well.
Note that you can get different "parts" of the path of your file. Have a look on this answer I always have a look on as well. Alternatively check the documentation for the for command typing for /? into the command-line.

finding textfile name by pattern and moving to newly created folder

I need some help with batch files as I have not done it before. I need to find textfiles with a matching pattern and move them to a class folder. I have a couple of folders that I am browsing through. Can anyone help me to modify the code to move the textfiles together?
This is what i research and piece together.
#echo off
setlocal enabledelayedexpansion
set file=name.txt
set foldername=class
set location=blockB
for /f "tokens=* delims=" %%a in (!file!) do (
set folder=%%a\public\
if not exists "%CD%\!folder!\!foldername!" (mkdir "%CD%\!folder!\!foldername!") ELSE (call)
dir /b "!folder!" | findstr /r /c"!location!"
)
my name.txt contains (adam,ben,charlie) 1 name per each line. So technically i want to move text files in (\adam\public) which contains blockB in the textfile name to a newly created folder call class (\adam\public\class). i want it to do the same for (\ben\public) and (\charlie\public). If I have any mistake in the way i code pls pardon me. Thanks.
Reason why I did not use a full path is because I am going to use it on different computers with same folder configurations.
I guess you are trying to accomplish this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=name.txt"
set "FOLDERNAME=class"
set "LOCATION=blockB"
for /F "usebackq eol=| delims=" %%F in ("!FILE!") do (
set "FOLDER=%%~fF\public"
mkdir "!FOLDER!\!FOLDERNAME!"
move "!FOLDER!\*!LOCATION!*.txt" "!FOLDER!\!FOLDERNAME!\"
)
endlocal

Change a file name in a batch script

I need to change a file name in a batch script.
Below is a sample I made
SET date = 20210803
SET job = 69187
cd "H:\arbortray foldher\"
for %%i in (*.txt*) do REN %%i randum_%job%-text-%date%%%i
It is not working; does nothing. I want it to change a specific file name from a generic version to one using the globally defined variables that are used through out the script. The file is already being moved from another program that makes the file into this folder. I can not include the variable in the file name at those steps. I want to include the commands as part of a larger script that does other things using the variables. Specifically, in this case I need the commands to rename the file from the generic version to one that includes variables defined earlier in the script. These variables change weekly.
The problems are:
A) Your variable names will have spaces in them.
B) The CD command needs a /d
C) The for in do has a bug which has to be worked around by changing the extension and restoring it later.
#echo off
SET date=20210803
SET job=69187
cd /d "H:\arbortray foldher\"
for %%i in (*.txt) do REN "%%i" "randum_%job%-text-%date%%%~ni.tmp"
ren *.tmp *.txt
echo done
pause
The spaces caused the issue and in the rename command you need double quotes to cater for spaces.
take a look at the cross-platform renamer tool..
your example insecure:
date (isn't good practica)
md itsdir.txt
ren %UNICODECHARS% %insecure?%
answer:
#echo off
chcp 65001 >NUL 2>NUL.
set "v_date=20210803"
set "v_job=69187"
set "v_dir=%cd%"
for /f "tokens=* delims=" %%s in ('dir /b /a-d "%v_dir%" ^| findstr /i /e /c:".txt"') do #ren "%v_dir%\%%s" "randum_%v_job%-text-%v_date%%%s"
forgot about /d and cd ))
maybay you mean "randum_69187-text-20210803-example.txt" in example result?

Batch Command to replace text in file

I know this question is asked many times, but I didn't get the answer for what I am searching.
I want to replace a pattern using windows .bat file.
I know how to replace X with Y.
But I am trying to replace say installPath with C:\Programfiles\Install\.
Here, I am facing issues as the new value string contains \ i.e special character.
Please let me know how I can replace this.
This works fine for me
set p=installPath
set p=%p:installPath=C:\Programfiles\Install\%
echo %p%
Followinf script will find the string in the file and replace with another string.
EX. "installPath" will be replaced with "C:\Programfiles\Install"
#echo off
for /f "usebackq tokens=*" %%a in ("test.txt") do call :Replace "%%a"
del "test.txt"
rename "newfile.txt" test.txt
exit /b
:Replace
set str1=%~1
set str1=%str1:installPath=C:\Programfiles\Install%
echo.%str1%>>"newfile.txt"
exit /b
Perhaps this tool might help you:
http://sourceforge.net/projects/fart-it/
This should work... By the way, this is my first post on this website.
The following uses delayed expansion so that you have two different 'variable symbols' to play with:
setlocal enabledelayedexpansion
set iPath=installPath
set input=C:\Programfiles\Install\
set p=!iPath:installPath=%input%!
Hope this helps

Resources