1

I am currently working on a batch program for a friend of mine that is going to be a tutorial for them once it's all finished.

I would like the program to create a sub-directory in the Documents folder, create inside a new .bat file, add a few lines of code to the new file, finally open the new file.

I know that the creation of a Folder/File in the Documents can be done without knowing the username of that computer such as C:\User\Thomas\Documents

The code just drops the creation of the File/Folder in the user's Documents bypassing that path. What code do I need to add to the program to make that happen?

I would like to also open a particular folder with batch code as well.

2
  • Can you rephrase the question? Consider breaking it into smaller pieces and making a list. Provide examples, if relevant, to make the question more understandable.
    – user308986
    Dec 31, 2019 at 20:08
  • I tried to clarify your text, but it is still confusing. Could you show us your code and what is its result?
    – harrymc
    Dec 31, 2019 at 21:41

2 Answers 2

1

The userprofile variable can be used for this.

There's a Number of 'fixed' environment variables that are static.

In cmd.exe type:

SET

and you'll get a list of all currently set variables - typically all the fixed variables.

The Documents folder is located at %userprofile%\documents by default.

Generally, though, it's better to create subfolders in your programs folder, relative to the program itself. Even if the folders moved, everything in it stays relative to your main program. However if things created outside your programs folder get moved, your program can no longer access it.

To achieve a relative filepath, in your Main program, before calling any functions:

Set Prog_Dir=%~dp0

This expands to the directory (folder) your program is in, and you then make any new folders / files needed within your programs folder.

IE:

IF not exist "%Prog_Dir%resources" MD "%Prog_Dir%resources"

Your subfolder will always be at "%Prog_Dir%resources".

Addition:
Not 100% clear what you're asking with regards to opening a specific folder, so I'm going to make an assumption:

To open a folder in the Windows Explorer GUI:

start explorer.exe "PATH"

where path is the directory pathway for the folder you wish to open.

E.g., to open the user's Documents folder:

start explorer.exe "%userprofile%\documents"

Alternately, you can view the contents of a folder within a command, using:

DIR
REM - Will display all folders (Directories) and files in the current working Directory.

Type DIR/? in cmd.exe for a full list of options (there's a lot).

To view just the Folders and subfolders of a particular directory:

TREE
REM - Will display a graphical representation of all Directories and Subdirectories Starting in the Working directory.
REM - A specific Folder to view can be specified using TREE:
TREE %userprofile%\desktop
1
  • for details on other information that can be obtained by expanding variables related to files or tokens, type: For/? in cmd.exe
    – T3RR0R
    Dec 31, 2019 at 21:11
0

My suggestion is to read the current user's path instead get %userprofile%, in the registry.

Get a complete/qualified path to UserProfile\Documents and create this folder:

  • For cmd/bat file:
@echo off 
for /f tokens^=3 %%i in ('reg query "HKCU\Volatile Environment"^|find "USERPROFILE"
')do MkDir "%%i\DOCUMENTS\FOLDER" && pushd "%%i\DOCUMENTS\FOLDER" && dir . && popd

  • For command line:
for /f tokens^=3 %i in ('reg query "HKCU\Volatile Environment"^|find "USERPROFILE"')do MD "%i\DOCUMENTS\FOLDER" && pushd "%i\DOCUMENTS\FOLDER" && dir . && popd

  • Here is a bat file that creates another bat file in a new folder in "~\Documents\" and save as: "~\Documents\Folder\New_Bat.cmd":
@echo off && setlocal enabledelayedexpansion

cd /d "%~dp0" && cls && title <nul && title ...\%~nx0 && color 0a
for /f tokens^=3 %%i in ('reg query "HKCU\Volatile Environment"^|find "USERPROFILE"
')do 2>nul MD "%%~i\DOCUMENTS\Folder" & pushd "%%~i\DOCUMENTS\Folder" && >.\NewBat.cmd ^
    (
     echo/ @echo off ^&^& setlocal enabledelayedexpansion 
     echo/ ^>nul color 9f ^&^& set "_msg=I'm here: "%%~dpnx0""
     echo/ set "_pp="c:\windows\system32\pathping.exe" 127.1 -q 01 -p 50 #nul"
       set "_esc_for1=for /f ^skip^=^4 %%%%a in ('echo=prompt $h^|cmd')#"
       set "_esc_for2=for /f ^eol^=*^delims^= %%%%A in ('cmd/v/u/c echo/%%_msg%%^<nul^|find /v /i "~"')@ "
     echo/ !_esc_for1:#=do! !_esc_for2:@=do! %%_pp:#=^>nul%% ^&^& set /p "=-^%%%%a%%%%A" ^<nul
     echo/ popd ^& endlocal ^&^& exit /b ^|^| goto :EOF
    ) && echo/ & if exist .\NewBat.cmd @where .:NewBat.cmd && endlocal && popd && exit /b || goto :EOF

  • This is the NewBat.cmd created in a new sub-folder under ~\Documments:
 @echo off && setlocal enabledelayedexpansion 
 >nul color 9f && set "_msg=I'm here: "%~dpnx0""
 set "_pp="c:\windows\system32\pathping.exe" 127.1 -q 01 -p 50 #nul"
 for /f ^skip^=^4 %%a in ('echo=prompt $h^|cmd')do for /f ^eol^=*^delims^= %%A in ('cmd/v/u/c echo/%_msg%^<nul^|find /v /i "~"')do  %_pp:#=>nul% && set /p "=-%%a%%A" <nul
 popd & endlocal && exit /b || goto :EOF


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.