Get name of last created folder on windows command line - windows

Is it possible to get the name of the last created folder in a variable on the windows command line?
Preferably natively, without using Cygwin etc. What I'm looking for is to run some command/batch script which gives me the LastCreatedDir for the path in which I currently am

from a batch file:
#echo off
FOR /F "delims=" %%G IN ('dir /O:-D /A:D /T:c /B') DO (
#set yourvariable=%%G
exit /b
)

Related

Getting Parent Directory for each file from Dir Output

I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause

How to return the newest file from each sub-directory in Windows command line using batch

I am using batch script for windows command line on windows 7. I am currently trying to get the newest file from each sub directory and print them to the screen. For example, if I have:
C:\Home\Hi\Folder1\a 01/05/2016
C:\Home\Hi\Folder1\b 01/10/2016
C:\Home\Hi\Folder2\x 03/05/2016
C:\Homeh\Hi\Folder2\y 03/1/2016
It would return: folders b and x.
I have written some script to attempt this, but it only returns the newest file in the last directory which in my example would be x. The script is:
FOR /R "C:\Users\Maxx\Desktop\tools" %%G in (.) DO (
Pushd %%G
FOR /F %%I IN ('DIR %%G /B /O:D') DO SET NEWEST=%%I
ECHO %NEWEST%
Popd )
Echo "back home"
If anyone knows how to get the newest file from each subdirectory that would be great.
Note: I have looked at various other examples such as: Generate a list of the newest file in each subdirectory in windows batch, which has been very helpful in building what I have now, but ultimately it did not work.
You need to apply delayed expansion as you are writing and reading variable NEWEST within the same block of code:
setlocal EnableDelayedExpansion
for /R "C:\Users\Maxx\Desktop\tools" %%G in (.) do (
pushd "%%~G"
for /F %%I in ('dir "%%~G" /B /O:D') do set "NEWEST=%%I"
echo !NEWEST!
)
popd
echo back home
endlocal
Alternatively, replace echo %NEWEST% by call echo %%NEWEST%%:
for /R "C:\Users\Maxx\Desktop\tools" %%G in (.) do (
pushd "%%~G"
for /F %%I in ('dir "%%~G" /B /O:D') do set "NEWEST=%%I"
call echo %%NEWEST%%
)
popd
echo back home
In addition, I improved quoting.
#ECHO OFF
SETLOCAL
FOR /R "C:\106x" %%G in (.) DO (
Pushd "%%G"
SET "first="
FOR /F "delims=" %%I IN ('DIR . /a-d /B /O:-D 2^>nul') DO IF NOT DEFINED first SET first=Y&ECHO %%~dpI
Popd
)
Echo "back home"
GOTO :EOF
For each directory, go to the directory, clear a flag first, scan the directory for files (not directories), basic format, reverse-date order, suppressing any "file not found" reports from empty directories.
On finding the first (ie youngest) file set the flag first and report the filename - or parts of the filename you require.
Once first has been set, the report mechanism will not be invoked as if defined works on the current value of the variable.
Please note that when specifying dates, it's advisable to state the format that you are using - or at least use a date where the day number is >12, which should be adequate. For instance, your date "01/10/2016" may be interpreted as Jan 10th or Oct 1st, depending on the respondent's locale. "13/10/2016" or "10/13/2016" would make the issue obvious.

Batch script listing files in a directory by appending to string

I am using a batch file and need to be able to list all JavaScript files found at a relative path to where the batch script is run. The final list needs to be formatted as:
"C:\path\to\file1.js" "C:\path\to\file2.js" "C:\path\to\file3.js" etc..
So far I have done:
SET files=""
FOR /f "delims=" %%i IN ('dir /b /s ".\path\to\files\*.js"') DO (
SET files=!files! "%%i"
)
ECHO Files found: %files%
However, with the script above, all I get as output is:
Files found: "" C:\path\to\last\file.js
It only outputs the last file that was found in the directory. I don't know what the issue is here as to why it only appends the last file.
Run the Batch file below exactly as appears here:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "files="
FOR /f "delims=" %%i IN ('dir /b /s ".\path\to\files\*.js"') DO (
SET files=!files! "%%i"
)
ECHO Files found: %files%
If the output is not what you want, execute the next command directly from the command-line:
dir /b /s ".\path\to\files\*.js"
If previous line show the same files than the Batch file, then the problem is related to dir command.

batch file to search sub directories

I have a script i am trying to alter to search multiple sub directories within a given path, then copy the files found to location create folder with the copied files in folder file was ran from. At the moment script works but does to search sub directories, How do i alter my script below please.
#echo off
set LIST= C:\batch\list.txt
set FILESPATH="C:\Test"
for %%i in ("%LIST%") do set DEST=%%~ni
for /F "usebackq delims==" %%i in (%LIST%) do (call :COPY_FILES "%%i")
:COPY_FILES
xcopy /qv %FILESPATH%\%1 .\%DEST%\*
I have tried to alter this line
for /F "usebackq delims=" %%i in ('dir /s /b "%LIST%"') do (call :COPY_FILES "%%i"), but gets an error that the file list.txt could not be found.
Thanks
Test this: it will only print the xcopy lines to the console.
If it seems right then remove the echo from the xcopy line and run it for real.
If the commands are not right then provide a sample of the lines to see which part is wrong.
#echo off
set "LIST=C:\batch\list.txt"
set "FILESPATH=C:\Test"
for /F "usebackq delims=" %%i in ("%LIST%") do (
echo xcopy /s/h/k/f/c/q/v "%FILESPATH%\%%i" "%%~ni\"
)
pause

batch file (windows cmd.exe) test if a file is a link

How can I test if a file is a symbolic link from a batch file (windows cmd.exe)?
(I would have thought that this was a well phrased question, but stackoverflow appears to want me to write some more, so here it is!)
dir %filename% | find "<SYMLINK>" && (
Do something
)
for /f "tokens=2delims=[]" %%a in ('dir /ad ^|find "<SYMLINKD>"') do echo Symlink: "%%a"
This doesn't work with [] in Symlink names.
Here is an even faster solution as it doesn't require a pipe and also allows access to the original file name either as a relative or absolute path:
#echo off
setlocal EnableDelayedExpansion
set ScriptPath=%~dp0
cd /d "%ScriptPath%"
set "r=%__CD__%"
set SearchTarget=SomePath
for /f "tokens=*" %%a in ('dir /s /b /a:l %SearchTarget% 2^>nul') do (
set "FullPath=%%a"
set "LocalPath=!:%r%=!"
rem local -> [absolute]
echo(!FullPath:%r%=! -^> [%%a]
)
If you don't want to search recursively then remove the /s option for the dir command.

Resources