cd Into a newly created folder with a random name - windows

I'm currently in the process of running a script that exports a registry key for Outlook profiles before deleting them.
My script currently achieves this - however I am trying to create a folder named Outlook_Backup under the user profile which is dated and has a timestamp.
Is there any way of being able to Create the Outlook_Backup directory and then cd to the Outlook_Backup-dd-mm-yy folder in this location despite having a different folder name each time the script is run?
Usually you could cd to Outlook_Backup but each folder will change such as Outlook_Backup-29-01-2019-26-08-91 and Outlook_Backup-29-01-2019-26-15-65
cd %userprofile% & mkdir Outlook_Backup & cd Outlook_Backup & mkdir Outlook_Backup-%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%
The code provided works, however is there a way to append something such as cd latest.Outlook_Backup or the latest created in the Outlook_Backup directory?

I totally get where you are comming from with this.
There are times you want to just paste one or several lines into a CMD Prompt CLIinstance and be done ( and might want to just, as I put it about my own such use of CMD CLI, "Be Lazy", not create permanent variables, or keep itall in one lineto easily give yo oyhers to paste on in to cmd or for whatever reason)
So you have a few options:
A: separate the cmd into separate lines, save the date in a variable and use throughout.
B: wrap everything in a loop ( which also means it can still be written to span multiple lines without them running imdividually as they are entered )
C: Use a loop at the end to find the name of the folder written each time.
All are completely valid methods.
I usually prefered option B when trying to keep things single lined as the loop allows me to stipl paste itnmulti-linee but get a more? when testing in case I missed something in my copy paste, and its easy to expand/collapse (IMO)
I'm on mobile, writing this and my kids just started waking up. Gotta get rolling.
I will paste option B in real quick if I can but not going to have time to write all 3 just this moment.
Oh, another thing, avoid CDing into locations if possible. Ymmv but often writing a cmd that generates the full path is more efficient and less likely to cause issues.
In any case:
For %%A IN ("%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%") DO ( cd %userprofile% & mkdir Outlook_Backup & cd Outlook_Backup & mkdir Outlook_Backup-%%~A & CD Outlook_Backup-%%~A
)
The above can be run across multiple lines as so:
For %%A IN (
"%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%"
) DO (
cd %userprofile%
mkdir Outlook_Backup
cd Outlook_Backup
mkdir Outlook_Backup-%%~A
CD Outlook_Backup-%%~A)
You don't neee those ChDir (CD) statements (as I mention above) and MkDir (MD) can create multiple levels of sub directories, so it would be pragmatic to write it as this:
For %%A IN (
"%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%"
) DO (
MD "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\"
CD /D "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\" )
Or as a single line:'
For %%A IN ( "%date:~-10,2%"-"%date:~7,2%"-"%date:~-4,4%"-"%time:~3,2%"-"%time:~6,2%"-"%time:~9,2%") DO ( MD "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\" & CD /D "%userprofile%\Outlook_Backup\Outlook_Backup-%%~A\" )

Related

Batch file to move between symmetric folder structures

I work in a project with symmetric folder structure, i.e:
Master
Folder1
Folder_A1
Folder2
Folder3
Slave
Folder1
Folder_A1
Folder2
Folder3
While I'm working, I usually change between master and slave to the same directories. I want to create a batchfile that helps me to switch only the maste/slave directory with this, i want something like this:
C:\Project\Master\Folder1\FolderA1>BatchFile
C:\Project\Slave\Folder1\FolderA1>
I plan to create 2 batches, toMaster and toSlave, that takes the current directory and replaces the "Master" String with the "Slave" and then move to that directory. Heres what I have so far in the toSlave batch:
#echo OFF
setlocal ENABLEDELAYEDEXPANSION
set word=Slave
set "str=%cd%"
echo %str%
set str=%str:Master=!word!%
echo %str%
pushd %str%
This seems to replace the strings correctly but when I run it from a terminal, it doesn't change my current directory. Any idea ?
Thanks
Your approach doesn't work, because you did a setlocal. When the batch file ends, an implicite endlocal is executed, which undoes pushd. There is another reason you shouldn't use pushd: it saves the current location on a stack and doing it too often, may lead to a stack overflow. Better use cd instead.
Your batch file is very overloaded. Most of the lines are not needed and you can do the rest in just one command.
All you need for toSlave.bat is:
#cd /d "%__cd__:\Master\=\Slave\%"
(or you use #Compo's "Toggle" approach. It's quite elegant, but it's your job then to verify you are in the correct folder)

Run program in subfolders

I'm trying to write a Windows batch script to run 2 programs through a bunch of folders. I'm not an expert at shell scripting but I try my best.
Here's what I'm trying to run through each folder...
The input for program1 is a .extension1 file which then produces a .extension2 file which then is run through program2 to generate what I need.
Before I run the script I cd into the folder. The programs are copied to the folder because they only work in the current working directory.
copy C:\program1 .
copy C:\program2 .
for %i in (*.extension1) do program1 "%i"
for %i in (*.extension2) do program2 "%i"
The data folder shown above contains hundreds of folders that I need to run the program in. I'd like to be able to do this in one big batch script.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\one"
PUSHD "%sourcedir%"
FOR /r %%a IN (*.ext1) DO (
PUSHD "%%~dpa"
ECHO(c:\program1 "%%~nxa"
popd
)
popd
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
In all probability, this is all you would need - or at least, this is a framework.
Note that the routine will simply ECHO the required commands. This allows a harmless test (on a representative sub-tree) to ensure that the process should work on the entire tree.
Changing ECHO(c:\program1 to c:\program1 should execute the first program on each .ext1 file in the sub-tree. It would be unusual for a program to check that the file exists within the same directory as the executable - if it won't take a path, then "the current directory" would be assumed.
You don't say whether the program program1 produces a file called whatever.ext2 from whatever.ext1 or whether it produces somethingradicallydifferent.ext2. In all probability, the same name would be used.
If that is the case, then to run the second program, simply add after
ECHO(c:\program1 "%%~nxa"
ECHO(c:\program2 "%%~na.ext2"
Otherwise, simply repeat the entire block, changing ext1 to ext2
(I'll assume you can figure out that I've abbreviated the extensions)
If, on the off-chance the program(s) need to be in the same directory, then replace
ECHO(c:\program1 "%%~nxa"
with
echo n|C:\program1 . >nul 2>nul
ECHO(program1 "%%~nxa"
(and ditto for program 2, obviously). Here the n is echoed into the copy, so that the copy will only take place once. This could be improved but is probably only a theoretical requirement anyway since it's 99.9999% likely that executing c:\program? will work quite happily.

Using a networked drive I am trying to access a folder using a variable. I only get the contents of the previous folder.

I have a location on a networked drive that gets a folder with the current date for every file. I am trying to access this location by temporarily mapping the drive (Net Use) and then using the date to fill in the folder name.
When I execute the code it is set to the previous directory. (A DIR listing will bring up the contents of that folder and the rest of the code to move files puts them in that location)
Any ideas?
Thanks
PAUSE
REM This is to move the Export file to the FTP Site.
Set Year_Mo_Da=%date:~10,4%-%date:~4,2%-%date:~7,2%
time /t
net use z: \\Network.com\Validation\2014
REM \%Year_Mo_Da% -- This is the folder name
cd /d z:\%%Year_Mo_Da%%
DIR
Copy Z:\*FileName_*.* Y:\NewLocation\TEST
net use z: /delete
time /t
PAUSE
You're changing directories but then copying from the root after all. #foxidrive removed the backslash but perhaps it wasn't noticed when you made your change?
cd /d z:\%%Year_Mo_Da%%
...you're in the subdirectory
Copy Z:\*FileName_*.* Y:\NewLocation\TEST
...but you copied from the root of Z: after all.
This would work (quotes can't hurt of course but for this example aren't required unless your filename contains spaces):
Copy Z:*FileName_*.* Y:\NewLocation\TEST
BTW I tend to use pushd and popd so I don't have to know the drive letter. Don't know if that helps you or not but you don't have to net use /d either, or care if Z: is in use for something else.
pushd \\Network.com\Validation\2014\%Year_Mo_Da%
Copy *FileName_*.* Y:\NewLocation\TEST
popd
You don't need doubled percents here:
cd /d "z:\%Year_Mo_Da%"
Depending on what you need to do - this may be the next command:
Copy "*FileName_*.*" "Y:\NewLocation\TEST"

I have a set of recordings with me from which i have to extract only a select few clips

I have about nearly 800 recording clippings from which i have to extract only a few select clips, the clips which i have to extract and store in another folder is a list that i should prepare on excel or on a notepad, whichever is easy to program with
To say my example would look like this
This is my folderlocated in a path called C:\Desktop\Recordings
20140404-204604_1622719993-all
20140404-204638_1622737834-all
20140404-204925_1634300477-all
20140404-205903_1654712140-all
20140404-210135_1664564521-all
20140404-210924_1698260169-all
etc. etc.
from where i have a notepad file called list.txt which will contain the data somewhat like this.
list.txt
1524824025
1524846905
1530242587
1555663573
1555663992
1555811052
1555820729
1555820601
I have to extract the clips in the above folder matching to the set of filenames i have in the notepad/excel above and paste/extract them on a different directory.
Is it possible ?
Place file.txt from notepad in C:\Desktop\Recordings and launch this.
It will show you the copy commands on the screen - remove echo and run it again to actually copy the files.
Replace copy with move if you want to move the files to the target folder, which must already exist.
#echo off
cd /d "C:\Desktop\Recordings" && (
for /f "usebackq delims=" %%a in ("file.txt") do (
echo copy "*%%a*" "c:\new\folder"
)
)
echo if the screen is empty then double check the path in the cd command.
pause

A 'cd ..' Alternative to Quickly Moving to Ancestor Directories in Windows Command Prompt

I use a Windows command prompt for primary navigation on my Window 7 development machine, and my daily process requires me to get from one location to another in the directory structure quite frequently. I notice that I use 'cd ..' frequently to move to ancestor directories in the directory structure. Is there any solution to reducing the typing cost of multiple sequential 'cd ..' commands? For instance, if I could type a command that would move me up x ancestors up into the directory structure, then I wouldn't need to type 'cd ..' x times.
The primary goal is to get around quicker and more efficiently, so other answers that solve the general goal are also appreciated.
Thanks.
#ECHO OFF
SETLOCAL
SET /a times=%1 2>NUL
IF NOT DEFINED times SET /a times=1
IF %times%==0 (CD "%~1") else (FOR /l %%i IN (1,1,%times%) DO CD ..)
GOTO :EOF
How about this - save it on your path and call it say cdx.bat
cdx n
with n numeric should rise n levels.
Otherwise, should go to directory nominated as %1
Yes - I'm aware that the evaluation isn't complete or bulletproof. That's an exercise for those who may be interested...
Use ..\ like so:
cd \
cd "Program Files"
cd "Common Files
cd ..\..\..\
cd
> C:\
You can also use doskey to create aliases, so rather than constantly typing iterations o ..\ you can use doskey like so:
C:\Users\Foobar\>doskey u2=..\..\
C:\Users\Foobar\>doskey u3=..\..\..\
C:\Users\Foobar\>u2
C:\>
Create a batch file called c.bat in c:\windows or somewhere else on the PATH
#echo off
cd ..
It's a start.

Resources