Windows Batch: getting Path without Shortening - windows

I searched now for several Hours and didnt find any fitting solution for me.
When I try to get the current Path of the Batch File, I use normaly %~dp0.
This will leads in Paths like: D:\VM\TUTORI~2\STARTS~1.BAT
However this is not an issue, if only doing File Management, but I want to use the Script to call Vagrant commands.
It happens that the Vagrant System doesnt like this shortened Paths.
As soon as the Path is short enough or when I call it from the CMD directly, it works just fine.
I'm now searching for a way to get any unshortened Path to my working Directory.
But whatever I tried so far didnt work and google only delivers me how people Shorten their Paths.
Is there a practical Solution to this Problem?
I want to set a Variable in my Script looking like this:
D:\VM\tutorialtest
EDIT:
According to the Accepted Answer the working snippet for me is the Snippet without the "-2" on the 2. For Loop. This will give me the Path no matter where the folder is.
setlocal EnableDelayedExpansion
set myPath=%~DP0
set myPath=%myPath:*\=%
set fullPath=
pushd \
for %%a in ("%myPath:\=" "%") do (
set thisDir=%%~a
for /D %%d in ("!thisDir:~0!*") do (
set fullPath=!fullPath!\%%d
cd %%d
)
)
popd
echo Full path: %~D0%fullPath%
Result:
D:\VM\tutorialtest

Try this:
#echo off
setlocal EnableDelayedExpansion
set "myPath=%~DP0"
set "myPath=%myPath:*\=%"
set "fullPath="
pushd \
for %%a in ("%myPath:\=" "%") do (
set "thisDir=%%~a"
for /D %%d in ("!thisDir:~0,-2!*") do (
set "fullPath=!fullPath!\%%d"
cd "%%d"
)
)
popd
echo Full path: %~D0%fullPath%
Post the result, please.
EDIT: Bug fixed
The problem is that names that have less than 2 characters are cutted! (my mistake).
I did some testing and it seems that for /D %%d in ("TUTORI~2*") ... also returns the full name of the folder, so thisDir variable is not required. You may modify the for %%a loop this way and get the same result:
for %%a in ("%myPath:\=" "%") do (
for /D %%d in ("%%~a*") do (
set fullPath=!fullPath!\%%d
cd %%d
)
)

Edit - Corrected the code which can work with path with or without spaces.
for /f "delims=" %i in ('dir ^| findstr /i /c:"Directory of"') do set mypath=%i
set mypath=%mypath: Directory of =%
Above codes will work if you type them directly in command console, if you want to use it in batch file use it as below.
for /f "delims=" %%i in ('dir ^| findstr /i /c:"Directory of"') do set mypath=%%i
set mypath=%mypath: Directory of =%
I believe you are in need of getting the current working directory(something like pwd in Unix). 'dir' command usually returns the current path details along with variuos other details. We just pick the line with the directory name (line with the tag "Directory of") and then in the second line we remove the tag "Directory of" (replacing it with none).
Refer the below link for more string manipulation techniques.
http://www.dostips.com/DtTipsStringManipulation.php
Sample output - typed on command console
C:\test\new folder>for /f "delims=" %i in ('dir ^| findstr /i /c:"Directory of"') do set mypath=%i
C:\test\new folder>set mypath= Directory of C:\test\new folder
C:\test\new folder>set mypath=%mypath: Directory of =%
C:\test\new folder>echo.%mypath%

Did you try :
set $path=%cd%
echo %$path%

If this behaviour occurs when you open a cmd window, then type echo %comspec% at the cmd prompt and see what it returns: if it has command.com as part of it then it has been altered.
Another possibility is that the shortcut you are using to open a cmd window has command.com in the properties to launch it.
If you are using a shortcut then try to open a cmd prompt from the Win+R hotkey and type cmd and enter. See if this behaviour has changed but also check the first point above once more.

Related

Syntax/Script to only select files created %date% (today) for batch file?

Created a batch file to go into our AutoCAD '.scr' files in a specific directory and that were created today. Im not sure if to do what i need, if i have to use FORFILES instead. The code works but not for files created today.
Ive tried making arguments such as
(%~ti :: FindSTR %DATE%)
and as suggested in the comments:
ECHO %~ti |FindSTR %DATE% && (echo today) || (echo other day)
to attempt too specify the date for the FOR command when looking for files to open and look for .open to replace as OPEN Y, but im not sure of the syntax OR where i can add it to the script.
The script seems to be going into all the files and saving even if the text DOESNT need to be changed, however changing it to only alter files that were created or last modified today would prevent that from happening.
#echo off
setlocal enableextensions disabledelayedexpansion
FOR /R "V:\PPY005\CSC\Script Maker" %%S in (*.scr) DO (
for /f "delims=" %%i in ('type "%%S" ^& break ^> "%%S" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%%S" echo(!line:.open=open y!
endlocal
)
)
I need it to only apply to files created today as it would create some confusion as we manually load these and it would be confusing if one would load that was dated in the title such as 9-10-IMP-EM.scr and appear on top as if it was created/modified for today.
I need the directory listed as this batch will not be in the root directory of the files being changed and the script is looking for files in that directory of that specific file type, we prefer putting scripts in a special directory and just referencing them from that networked location such as opening a linked location from a document or using a batch file that will have a menu to select and run specific batch files referenced in (dont need help with that, i know how to do that)
I am kinda pulling out my hair with this as I cannot find documentation online on how to do this with the FOR command. Im a beginner so to say at Batch files, ive written several at this point but im not sure how to do this.
I understand that this MIGHT be easier to do via Powershell, but the company I work for doesnt allow us to use powershell for security reasons.
You can use the FORFILES command to select files based on the date. You can then parse the output of the FORFILES command with a FOR /F command that then nests right into your original code.
#echo off
setlocal enableextensions disabledelayedexpansion
FOR /F "delims=" %%G IN (
'FORFILES /P "V:\PPY005\CSC\Script Maker" /M *.scr /D +0 /C "cmd /c echo #path"'
) DO (
for /f "delims=" %%H in ('type "%%~G" ^& break ^> "%%~G" ') do (
set "line=%%H"
setlocal enabledelayedexpansion
>>"%%~G" echo(!line:.open=open y!
endlocal
)
)

I need a batch file to generate a list with *only* file names

I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.

Assign content of DIR to a variable

I wrote the following cmd
DIR /B "%appdata%\Mozilla\Firefox\Profiles"
which returns the following single folder which exists at that dir location
4jktnrk2.default
I wish to store 4jktnrk2.default in a variable.
I tried the following
SET A=DIR /B "%appdata%\Mozilla\Firefox\Profiles"
and
SET A="DIR /B %appdata%\Mozilla\Firefox\Profiles"
However neither of these work.
I also think its best to not use a FOR loop as there is only One folder in that directory.
#echo off
setlocal enableDelayedExpansion
set "dir_c="
for /f "delims=" %%a in ('DIR /B "%appdata%\Mozilla\Firefox\Profiles"') do (
if "!dir_c!" equ "" (
set "dir_c=%%~a"
) else (
set "dir_c=!dir_c!;%%~a"
)
)
echo %dir_c%
You have no other option but to use FOR /F.There's no unix style for assigning the result of command to variable in batch.
Excuse me. I think that the real goal of this request is to "Get a single folder in a variable". The code segment below do that:
for /D %%a in ("%appdata%\Mozilla\Firefox\Profiles\*") do set "A=%%a"
The natural way of process folders in a Batch file is via for /D command (the same way than the natural way to process files is via plain for command). The for /F ... in ('dir /B ... command is a less efficient method that is used just when for or for /D commands don't provide the required results.
You really should use FOR /D or FOR /F as Aacini and npocmaka have suggested.
If you insist that you don't want to use FOR, then you will have to resort to a temporary file:
dir /b "%appdata%\Mozilla\Firefox\Profiles" >folder.tmp
set /p "a=" <folder.tmp
del folder.tmp

Windows batch - match folders with a dot (.) in the folder name

I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa

Batch File - FOR LOOP

Let me set the stage of my issue. I have a folder (FOLDER_ABC). Within that folder, there is a folder for my application, including a unique and always changing version number (application-v1.3.4). Within that folder, there is the application (application-v1.3.4.exe) - which will also change periodically.
C:\FOLDER_ABC\application-v1.3.4\application-v1.3.4.exe
In this section below I create a directory listing of the FOLDER_ABC for any folders starting with application* and store that folder name into a file called directory.txt. I then create a perameter and store that directory into it. I'm doing it this way, versus applying the direct full path to the directory or file, since the versions will change and I don't want to hard code the batch script.
cd C:\FOLDER_ABC\
dir application* /b /ad>"C:\FOLDER_ABC\directory.txt"
set /p verdir= <C:\FOLDER_ABC\directory.txt
Here is my issue. In the section below, I'm trying to get my batch script to run the application*.exe file, and continue on with my batch file. It currently runs my application, but it hangs and doesn't continue the rest of my batch script. I'm really new to all this coding so I appreciate the help. I assume it could be something related to me not closing the FOR loop properly? How can I get it to continue on to :FINISH?
cd "C:\FOLDER_ABC\%verdir%\"
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO %%G;
:FINISH
ECHO THE END
exit
Figured it out, but didn't have enough StackOverflow credits to answer my own question. My solution is listed below. Thanks everyone. You pointed me in the right direction.
cd "C:\FOLDER_ABC\%verdir%\"
FOR %%G in (*.exe) DO START %%G
you can try:
FOR /f "delims=" %%G IN ('dir /b /a-d *.exe') DO start "" "%%~G"
I'm not sure if this is your problem, but it is possible that the ; is causing problems. You do not terminate commands with ; in batch files.
There is no need for a temporary file. Your code can be greatly simplified with FOR:
pushd c:\folder_abc
for /d %%F in (application*) do cd "%%F"
for %%F in (*.exe) do "%%F"
:FINISH
ECHO THE END
exit /b
Try using the START command:
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO START %%G;
There may be other better ways if achieving what you want, for example, if you know that the exe always has the same name as its directory, and that there will be only one such directory, you could do the following:
FOR /D %%i in (application-v*) DO START %i\%i.exe
UPDATE
From comments:
My only issue, which I just realized, is that the application folder and application name are not always identical.
In that case, you could try something like:
for /d %%i in (application-v*) do for %%j in (%%i\*.exe) do start %%j

Resources