Batch file changes path for startup of all shortcuts - powershell

So what my batch file does currently is it asks for name of new path and what the old one was and then it copies the old files contents and puts them in the new one.
What i want to do after this is change the path of all files that end with .lnk
and change theyr START IN and TARGET from old to new.
I think what would be possible is also to just make the target of new link to the .exe file with same name.
#Echo Off
Set /P "oldname= Old name: "
Set /P "newname= New name: "
md "%newname%"
XCopy "%oldname%" "%newname%" /E /I
FOR /f "usebackq delims=|" %%f IN (`dir /b "%newname%\*Shortcut*"`) DO (
SET shortcut=%%f
mklink /d %shortcut% %shortcut: .lnk=%
)
pause

Related

How to create directory from URL path separator (forward slash) when that is present in FileName coded %2f or %2F

As I mentioned in Q-title, I have setup a sublime settings auto-syncing process to Github Gist of my choice when I have passed correct Access Token.
Now this process syncs any file that is present inside a folder(s), considering the relative paths, as DirName%2FFileName.extension where DirName and/or FileName may contain singular spaces(%20) between words(if >2 words in either).
Now when I download that gist thro' DownloadZIP button, it ofcourse downloads the whole gist with multiple files as single zip, which after download can be extracted into it's folder in local system(Windows 10/11 OS).
So, the question finally: When I have downloaded such zip and extracted into folder that contains one/multiple files that have %2f or %2F in their names, how do I create a directory from those filenames and move those files into those directory thro' a batch file if possible, or a PowerShell command, but runnable from Batch script, and then also rename those files to remove all DirName%2f parts from their names ? I know about mv and md commands but how do I create directory from URL coded character group and do those operations ?
Note: DirName%2f parts could be occurring in multiples like DirName1%2fDirName2%2f... if the file that is being synced from local is inside mainfolder/subfolders... relatively.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "#name1=DirName%%2f"
SET "#name2=DirName1%%2fDirName2%%2f"
SET "#name3=DirName1%%2FDirName2%%2F"
SET "#2f=%%2f"
SET "#subs=ABC"
SET #
FOR /f "delims=" %%e IN ("%#2f%") DO (
FOR /f "delims=" %%w IN ("!#name1:%#2f%=%#subs%!") DO ECHO REN "%#name1%" "%%w"
FOR /f "delims=" %%w IN ("!#name2:%#2f%=%#subs%!") DO ECHO REN "%#name2%" "%%w"
FOR /f "delims=" %%w IN ("!#name3:%#2f%=%#subs%!") DO ECHO REN "%#name3%" "%%w"
)
ECHO ===================
FOR %%b IN ("%#name1%" "%#name2%" "%#name3%") DO (
SET "#name=%%~b"
FOR /f "delims=" %%e IN ("%#2f%") DO (
FOR /f "delims=" %%w IN ("!#name:%#2f%=%#subs%!") DO ECHO REN %%b "%%w"
)
)
GOTO :EOF
This demo shows how in batch.
You can try :
[System.Web.HttpUtility]::UrlDecode("DirName%2FFileName.extension")
it gives :
DirName/FileName.extension
Then you can use the CmdLets with noun "Path" to manage your file names.
Get-Command -Noun path
If it exists some / in place of \, Powershel should manage them.
The to work with files and folders is given with CmdLet with noun Item :
Get-Command -Noun item
For example to create a directory :
New-Item -name toto -ItemType Directory

Trying to rename files based on file size

I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like
123456789 movie.mp4
987654321 movie2.mp4
Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the
corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?
I do some test with my mp4, and the code works, and for you perform your test, you w´ll need change/put this 2 line above with the path to your folder/directory (one to keep and another to compare), by replacing in the line code is like this:
`set "_target_to_keeped=C:\Users\ecker\Videos\Target"`
`set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"`
You need add the folder where are files to keep and files to rename (if size+name match) on same lines where are the 2 lines code up in this test (sorry not explain in good English, my English is not help me). By now, is late 01:53, i need sleep... yep! so, have nice code!
#echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0"
set /a _cnt_in_looping= 1 - 1
set /a _cnt_files_size= 1 - 1
set "_target_to_keeped=C:\Users\ecker\Videos\Target"
set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"
cd /d "!_target_to_keeped!"
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4" 2^> nul ^| find "" /v /c') do set _cnt_in_looping=%%i
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4"') do (
set "_file_now_keep=%%i"
set "_file_now_keeped=%%~zi %%i"
call :_to_compare_:
)
set /a _total_files_renamed=!_cnt_in_looping! - !_cnt_files_size!
set /a _total_files_n_chang=!_total_files_renamed! - !_cnt_in_looping! * -1
echo/Total of files renamed = !_total_files_renamed!
echo/Total of files n chang = !_total_files_n_chang!
endlocal
goto :_end_of_file_:
:_to_compare_:
if not exist "!_file_now_keep!" exit /b
for /f "tokens=*" %%I in ('^<nul dir /o-d /on /b "!_file_now_keep!"') do (
set "_file_now_compare=%%~zI %%I"
set "_path_now_compare=%%~dpI"
if "!_file_now_compare!" == "!_file_now_keeped!" (
rename "!_path_now_compare!\%%I" "%%~zI %%I"
echo/ rename "%%~I" "%%~zI %%I"
if ["!errorlevel!"]==["0"] call set /a _cnt_files_size=!_cnt_files_size! + 1
timeout /t 10
)
)
exit /b
:_end_of_file_:

How to Copy files that are in a directory to another directory recursively in Windows?

I have to create an script to copy files from a folder structure to other.
My source folder structure is similar to this:
-RootFolder
--ParentFolder1
--SubParentFolder1
--ToCopy
/*Here are the files to copy*/
--SubParentFolder2
--ParentFolder2
--OtherSubParentFolder
--ToCopy
/*Here are the files to copy*/
--ParentFolder3
--OtherSubParentFolder2
I want to copy the files that are in the "ToCopy" folders, into another folder, with this structure:
Destination folder structure:
--TargetDirectory
--SubParentFolder1
//Here the files that were in the ToCopy folder inside the SubParentsFolder1
--OtherSubParentFolder
//Here the files that were in the ToCopy folder inside the OtherSubParentFolder
Notice that I use the name of the "ToCopy" parent folder in the destination subfolders.
I know how I would do this with code (like C#), but I am at a lost on how to achieve it with a Batch file. Is it even possible? Or I would need to use something like powershell?
How can I copy my files following the structure I described?
I think, this should work...
$Folder= gci -path "d:\pstest" -recurse -Filter "ToCopy" | where { $_.psiscontainer }
Foreach ($Foldername in $Folder) {
$Destinationfolder=$Foldername.Parent
copy-item $Foldername.fullname -Destination "d:\Outputfolder\$Destinationfolder" -recurse
}
Hi to follow is a script I hacked away (via help from stack overflow), that reads the files from a txt document, then requests destination folder input and also src folder name it then just goes and recursively copies all the files to the new folder without keeping the old subfolder structure.
I will update this in future with the link to the person that I got the base template from for the admin area, but to keep in mind once you click that Batch can run as though it was a php script then everything makes sense. Took me whole day to research every command and alternative on SS64.com
Major thing to note is the pushd "%~dp0" this I use to make sure batch always uses my current directory as root.
As said I will do a proper write up on this and further stream lining since I am using it actively for moving files during a woocommerce shop update. P.S. the text file name should be entered without the .txt extention and every file name should start on a new line. Also if the destination directory does not exist it will create it. Use excel maybe to list the names then for renaming could output to new column and compile the batch rename command copy to new batch run first batch to fetch files and second batch to rename to preferred title, I do it in steps to keep my sanity.
Sorry was just a example of how I use it, but yes go ahead and enjoy hope this works for you.
#echo off
CLS
setlocal EnableDelayedExpansion
REM Changes root path to relative of current bat folder
pushd "%~dp0"
REM finds files in provided .txt file and copies them to destination directory
REM CHECK FOR ADMIN RIGHTS
COPY /b/y NUL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
IF ERRORLEVEL 1 GOTO:NONADMIN
DEL %WINDIR%\06CF2EB6-94E6-4a60-91D8-AB945AE8CF38 >NUL 2>&1
:ADMIN
REM GOT ADMIN RIGHTS
COLOR 1F
ECHO Hi, %USERNAME%!
ECHO Please wait...
set /p DEST_DIR="Copy files to:"%=%
set /p SEARCH_DIR="Copy files from:"%=%
#echo.
#echo Please check folder name for accuracy.
#echo Copy files to: %DEST_DIR%
#echo Copy files from: %SEARCH_DIR%
set /p CORRECT_FOLDERS="Are these correct? (please check spelling) y/n:"
if '%CORRECT_FOLDERS%'=='y' GOTO:YES_ANSWER
if '%CORRECT_FOLDERS%'=='n' GOTO:NO_ANSWER
COLOR 2F
ECHO.
PAUSE
GOTO:EOF
:NONADMIN
REM NO ADMIN RIGHTS
COLOR 4F
ECHO.
ECHO PLEASE RUN AS ADMINISTRATOR
ECHO.
pause
GOTO:EOF
:YES_ANSWER
#echo.
#echo you answered yes
#echo.
if exist %DEST_DIR% GOTO:READ_DATA
if not exist %DEST_DIR% md %DEST_DIR%&GOTO:READ_DATA
PAUSE
:NO_ANSWER
#echo.
#echo you answered no
set /p TRY_AGAIN="Try again? y/n:"
if '%TRY_AGAIN%'=='y' GOTO:YES_ANSWER
if '%TRY_AGAIN%'=='n' GOTO:EXIT_PROGRAM
PAUSE
:EXIT_PROGRAM
#echo.
#echo "So Sorry"
PAUSE
GOTO:EOF
:READ_DATA
#echo.
set /p GET_FILENAMES="What is the name of the text file your filenames are stored in?"%=%
if exist %GET_FILENAMES%.txt #echo We will now read and copy the files for you, have some coffee might take awhile & GOTO:WRITE_DATA
if not exist %GET_FILENAMES%.txt #echo Filename does not match, please type only the name without .txt extention & GOTO:READ_DATA
PAUSE
:WRITE_DATA
#echo.
#echo reading file name...
for /f "usebackq delims=" %%a in ("%GET_FILENAMES%.txt") do (
for /r "%SEARCH_DIR%" %%b in ("%%a*") do (
#echo Copy Started...
copy "%%b" "%DEST_DIR%\%%~nxb"
)
)
#echo Copy finished, please review actions. Lekker Man.
PAUSE``

robocopy file structure - rename file at destination if its newer

I would like to robocopy a directory and it's subdirectories to another directory. If a file at source is newer then I would like to make a copy of this file by adding a date/time stamp at end of the filename at destination and do the copy to destination.
I do not see any switches in the robocopy to do this. Can someone guide me how to do this.
Robocopy doesn't have a renaming switch, but you can use the rename command on the resulting files to add timestamps. Here's an example batch file:
#echo off
for /f "tokens=1-3 delims=. " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
dir Directory1\ /b > list
robocopy Directory1\ Backup\
for /f %%f in (list) do rename Backup\%%f %%~nf%mydate%_%mytime%%%~xf
Note that you will need to change the delimiters of the date depending on which country standard you follow. You can get that by executing date /t

Batch script that separates that creates folder by month

#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM Set variables
SET SOURCE=C:\My WebEx Recordings
SET DEST=\\XXXRD12\c$\WebExVideoArchive
SET 7ZIP=C:\Program Files\7-Zip\7z.exe
REM Compress local files with 7zip
ECHO ---------------------------------------------------------
ECHO BEGINNING VIDEO COMPRESSION OPERATIONS
ECHO ---------------------------------------------------------
CD /D "%SOURCE%"
FOR %%f in ("*.wrf") DO (
SET FILENAME=%%~nf
ECHO Compressing !FILENAME!
"!7ZIP!" a -t7z -aoa "!FILENAME!.7z" "%%f"
)
REM Copy compressed files
ECHO ---------------------------------------------------------
ECHO COMPRESSION COMPLETE - BEGINNING COPY OPERATIONS
ECHO ---------------------------------------------------------
REM XCOPY <source> <destination> <options>
XCOPY "%SOURCE%\*.7z" "%DEST%" /Y /V /I /R
REM Confirm successful copy, then delete originals
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Originals...
DEL /Q "%SOURCE%\*.*"
)ELSE (
ECHO Error Detected During Copying. Please try again...Press Any Key to Exit
Pause
)
I am trying to edit this code to when we run the batch file it creates a folder based off the month of our file format. We ran this script a whole lot but forgot to make a folder called April now we have May mixed in with April.
The files are formatted like
Username-R705-2011.05.04-1601-Disconnected.7z
I was wondering if there is anyway it can go off the .04 and make a folder for that month so it will automatically put it in the folder it is needing to go into, so it will be easy to search for by month.
** would it be possible if we can not use the format that we format our files in to have it sort by file creation.
Erase everything below (and including) the line REM XCOPY <source> <destination> <options> and replace it with the following:
for /F "usebackq delims=" %%a in (`dir /b "%SOURCE%\*.7z"`) do (
SET CURRENT_FILE=%%a
REM Extract the month.
for /F "usebackq tokens=3 delims=-" %%i in ('!CURRENT_FILE!') do (
SET CURRENT_FILE_DATE=%%i
SET FILE_MONTH=!CURRENT_FILE_DATE:~-2!
SET MONTH_DEST=!DEST!\!FILE_MONTH!
)
XCOPY "%SOURCE%\!CURRENT_FILE!" "!MONTH_DEST!\" /Y /V /I /R
REM Confirm successful copy, then delete original
IF %ERRORLEVEL% EQU 0 (
ECHO Copy Operation Successful. Removing Original...
DEL /Q "%SOURCE%\!CURRENT_FILE!"
)ELSE (
ECHO Error while copying "%SOURCE%\!CURRENT_FILE!.
)
)
This code goes through every file matching %SOURCE%\*.7z, extracts the month, and then copies the file to %DEST%\<month>. Month is just the 2-digit number from the filename.
Seems like you have a folder full of files with different numeric-month values embedded in the filename. I was thinking of extracting that value from each file, but it would be more straightforward to use brute force wildcards with 12 different XCOPY commands:
XCOPY "%SOURCE%\*-*-20??.??.01-*.7z" "%DEST%\01" /Y /V /I /R
XCOPY "%SOURCE%\*-*-20??.??.02-*.7z" "%DEST%\02" /Y /V /I /R
[...]
XCOPY "%SOURCE%\*-*-20??.??.12-*.7z" "%DEST%\12" /Y /V /I /R
I may be misunderstanding the question but if you can make some assumptions about the filenames, this would work.