Navigating to Desktop - windows

I'm having trouble getting my bat file to work correctly. I run two hard drives on my computer and my desktop is on my E: drive. So %Userprofile%\Desktop navigates me to my desktop on E: , but when put into my bat file It tries to create another folder on my C: Drive called Desktop. Exactly the same spelling. But I will be using this on another computer That may or may not have their desktop on their C: drive.
I am copying a file to another folder
xcopy /s "%~dp0\Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles" /Y
This will attempt to create a Folder called Desktop and drop the exe into that.
The bat is ran on a USB hence the "%~dp0"

Your xcopy command has a trailing backslash included in %~dp0 so there is one too much. Replace with:
xcopy /s /Y "%~dp0Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles\"
If you relocated your Desktop from the normal position you have to lookup the actual location in the registry (or PowerShell/vbscript user shell folders)
If the Desktop is not relocated, this batch will nevertheless get the proper location:
Set "Key=HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Set "Val=Desktop"
For /F "Tokens=2*" %%A in (
'Reg Query "%Key%" /v "%Val%" 2^>Nul ^| Find "%Val%" '
) Do Set "Desktop=%%B"
Echo 'Desktop' location: %Desktop%
The better way to evaluate the Desktop on varying windows systems is to use eryksuns version:
#Echo off
for /f "usebackq delims=" %%a in (
`powershell -c "[environment]::GetFolderPath('Desktop')"`
) do set "desktop=%%a"

Related

Copy User Subfolders Using Robocopy

I am trying to copy the Normal.dotm from all user's C:\Users\%username%\AppData\Roaming\Microsoft\Templates to C:\Temp\%username%. I'm specifically trying to backup the data before a reinstall as part of an SCCM Task Sequence. Any thoughts?
I have tried a few different scripts using robocopy and it either gets stuck in a loop or only copies one directory.
robocopy C:\Users\%username%\AppData\Roaming\Microsoft\Templates\. C:\Temp\%username% /s /create
Only copies directory cmd is run as:
robocopy C:\Users\ C:\Temp\ /s /xjd normal.dotm
Creates loop and creates C:\Users\Application\Data\Application Data\ forever
The account running this will need to be an Administrator in order to access everyone's directories. When you believe the correct commands are being created, remove the lower case echo from them.
#ECHO OFF
FOR /F "delims=" %%f IN ('DIR /S /B "C:\Users\Normal.dotm"') DO (
SET "TDIR=C:\temp%%~pf"
IF NOT EXIST "%TDIR%" (echo MKDIR "%TDIR%")
echo COPY "%%~f" "%TDIR%"
)
You might use a different way:
#echo off
setlocal EnableDelayedExpansion
for /R "C:\Users\" %%A IN (Normal.dotm) do (
set "fpath=%%~fA"
if not "!fpath:\AppData\Roaming\Microsoft\Templates\=!" == "!fpath!" (
rem Find username:
for /F "tokens=3 delims=\" %%B IN ("%%A") do (
set "current_username=%%B"
)
rem Copy files:
copy "!fpath!" "C:\Temp\!current_username!\"
)
)
That, of course, requires administrator privileges, you cannot enter other users directories without admin privileges. Right-click on your file and select 'Run As Administrator'.

CMD batch file for user profile migration

I am working on a refresh project, and I am rebuilding and swapping out a lot of computers. When doing a reimage, I have a script I can run which will copy the Desktop, Favorites, and Documents from all user profiles to a network share.
What I don't have yet, is a script for hardware replacements (vs a reimage) that will transfer data directly from the old computer to the new one. As with my other script, I need it to transfer data from the Desktop, Favorites, and Documents folders for all user directories present on the old machine. If I wanted to go a step further, I might exclude specific user profiles such as Public, Default, my own profile, etc, but a more basic script would still work.
I have done some batch scripting on my own, but I'm not sure what the syntax needs to be to have it cycle through all the user profiles on a remote machine.
Here is what I have tried so far:
#echo off
Set /p remotepc=Enter remote hostname:
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Desktop "C:\Users\%%~nxD\Desktop" /H /E /Y /K /I /R /C)
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Documents "C:\Users\%%~nxD\Documents" /H /E /Y /K /I /R /C)
for /D %%D in ("\\%remotepc%\USERS\*") do (xcopy \\%remotepc%\Users\%%~fD\Favorites "C:\Users\%computername%\%%~nxD\Favorites" /H /E /Y /K /I /R /C)
pause

Deleting all files and directories from desktop except .lnk

Im trying to write a script for keep clear my desktop. I want to delete all files and directories except the shortcuts.I use Windows 10. My batch code is the following:
#echo off
COLOR 0E
cd "C:/Users/DA/Desktop"
FORFILES /S /C "if #ext!=lnk del /F /Q /S"
rd /S /Q "."
pause
exit
Maybe it is a dumb error, but Im a newbie in Windows command line. Thanks in advance.
There are several issues in your code:
You must precede the command line after the /C switch of forfiles with cmd /C, because you are using internal console commands (if, del). If you omit cmd /C, forfiles tries to find a program file named if, which does not exist.
There is no comparison operator != for the if statement. You mean not equal, so you need to state if not <expression1>==<expression2> instead.
The #ext variable expands to the file extension enclosed in quotation marks, so you need to state them around lnk also. Since the "" are in the quoted command line behind forfiles /C, you need to escape them like \" in order to establish literal " characters.
You forgot to specify what to delete at the del command.
The switches /S of forfiles and also del mean to process also items in sub-directories, but I assume you do not want that, because you want to clean up your Desktop directory.
There is the rd command, so I assume you want to remove any directories from the Desktop either. However, rd /S /Q "." tries to remove the entire Desktop directory (which will fail as your batch file changes to that directory by cd). I would put the rd command into the forfiles command line as well, because there is the possibility to check whether or not the currently iterated item is a file or a directory (forfiles features the #isdir variable for that purpose).
The cd command works only if you are running the batch file from the same drive where the Desktop directory is located (unless you provide the /D switch). I would go for the pushd command, which changes to the Desktop directory temporarily, until a popd command is there.
Instead of hard-coding the location of the Desktop directory, I would use the built-in environment variable %USERPROFILE%, which points to the user profile directory of the currently logged on user, where the Desktop directory is located in.
The exit command without the /B switch does not only end the batch file, it also terminates the command interpreter instance the batch file is running in. This does not matter when you run the batch file by double-clicking, but it does matter when you execute it within command prompt.
Here is the corrected and improved code:
#echo off
title Clean Up Desktop & rem // (this is the window title, just for fun)
color 0E
pushd "%USERPROFILE%\Desktop" || exit /B 1 & rem // (the command after `||` runs if `pushd` fails, when the dir. is not found)
rem /* Here you can see how to distinguish between files and directories;
rem files are deleted with `del`, directories are removed with `rd`.
rem The upper-case `ECHO`s are there for testing purposes only;
rem remove them as soon as you actually want to delete any items: */
forfiles /C "cmd /C if #isdir==FALSE (if /I not #ext==\"lnk\" ECHO del /F /Q #relpath) else ECHO rd /S /Q #relpath"
pause
popd & rem // (this restores the previous working directory)
exit /B & rem // (this quits the batch file only; not necessary at the end of the script)
You can try something like that :
#echo off
COLOR 0E
CD /D "%userprofile%\Desktop"
Rem To delete folders
for /f "delims=" %%a in ('Dir /b /AD ^| find /v "lnk"') do echo rd /S /Q "%%a"
pause
Rem To Delete files
for /f "delims=" %%a in ('Dir /b ^| find /v "lnk"') do echo del /F /Q /S "%%a"
pause
exit
NB: When your execution is OK, just get rid of echo command
You can use the for and if commands to accomplish this:
#echo off
COLOR 0E
cd C:/Users/DA/Desktop
for /d %x in (*) do #rd /s /q "%x"
for %i in (*) do if not %i == *.lnk del "%i"
pause
Pretty simple and works great.
Make sure that %i and %x are in "".

Copy files from folder of lnk files back into folder

I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal

How to finish my batch file code?

I want to do following in batch file:
I want to open pdf, if it exist on usb. If this file don't exist, start pdf from another location, if this exist.
e.g.
:: Searchs USB Drive Letter
For %%I In (G,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) Do If Exist "%%I:\Document1.pdf" (
xcopy /H /Y /C /R "%%I:\Document1.pdf" "%tmp%"
start %tmp%\Document1.pdf
)
I want to to open pdf document (from USB) if inserted. Once this .bat launched, and usb will inserted, launch pdf from temp folder. I want this without any error messages on batch.
From what you have posted, I can only guess you are trying to run your batch file when a USB drive is inserted?
I'm pretty sure there is no native way to do this in batch, so I can only suggest you use an infinite loop on your batch and keep it running all the time, to detect any that get plugged in.
:LOOP
:: Searchs USB Drive Letter
For %%I In (G,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) Do (
If Exist "%%I:\Document1.pdf" (
xcopy /H /Y /C /R "%%I:\Document1.pdf" "%tmp%"
start %tmp%\Document1.pdf
)
)
REM Use timeout /t 5 instead of ping if you have Vista or above
ping 127.0.0.1 -n 5
goto :LOOP
and rather than loop all possible drive letters, I would suggest changing your for loop to this instead, so it only looks at drives that currently exist
for /f "skip=2 delims=" %%I in ('wmic logicaldisk get caption') do (
I have skipped the first 2 lines, as the first line is the column title and the second is 99% sure to be the C:\ drive which you didn't include in your script.

Resources