What is wrong with this batch copy code?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Danjor777
Posts: 1
Joined: 11 Oct 2016 16:26

What is wrong with this batch copy code?

#1 Post by Danjor777 » 11 Oct 2016 16:29

As you may know the profile name in the Firefox directory is randomly generated, and I need that pathway. Is there a way to put this "unknown" name into a variable? %appdata%\mozilla\firefox\profiles\unknown profile name

I tried this:

set parentfolder=%APPDATA%\Mozilla\Firefox\Profiles\
for /f "tokens=*" %%a in ("dir /b "%parentfolder%"|findstr "*"") do set profilename=%%a

but didn't work out

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: What is wrong with this batch copy code?

#2 Post by foxidrive » 12 Oct 2016 01:56

I've made some changes and added debugging lines for you to try out.
Not all the changes were needed and I'll outline the changes I made:

1) The parentfolder variable is enclosed in double quotes to protect against poison characters in the username etc.

2) The tokens=* would work fine in this situation but you will be caught in the future and wonder why spaces before the string are removed. The delims= prevents this from happening and it will preserve any leading whitespace characters.

3) The command inside the 'for' brackets requires tickmarks when using a command in this way. There are various formats/ways of using the 'for' command but this code works here in a single profile situation.

4) The profilename variable is enclosed in double quotes for the same reason as point 1). It helps to make batch code reliable if you adopt it as a routine way of using environment variables and paths, filenames etc.

Code: Select all

@echo off
set "parentfolder=%APPDATA%\Mozilla\Firefox\Profiles"
dir /b "%parentfolder%" & pause
for /f "delims=" %%a in ('dir /b "%parentfolder%"') do set "profilename=%%a"
set profilename
pause & goto :EOF

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: What is wrong with this batch copy code?

#3 Post by Compo » 12 Oct 2016 02:13

To do what you were trying to do then this should be what you needed:

Code: Select all

@Echo Off
Set "MFP=%AppData%\Mozilla\Firefox\Profiles\"
If Not Exist "%MFP%" Exit/B
For /D %%A In ("%MFP%*") Do  Set "PPath=%%A" & Set "PName=%%~nxA"
Echo(Profile Path is %PPath%
Echo(Profile Name is %PName%
Timeout -1 1>Nul
Exit/B

However, the default profile name is only random if you start Firefox for the first time without using the Profile Manager and creating one yourself in your chosen location with your chosen name. (The profile folders can be put anywhere and a user can have many of them).

The following should echo each profile path one at a time:

Code: Select all

@Echo Off
SetLocal
Set "PF=%AppData%\Mozilla\Firefox\"
If Not Exist "%PF%" GoTo :EndIt
For /F "Tokens=1* Delims==" %%A In (
        'FindStr/R "^Path=[A-Z]*" "%PF%Profiles.ini"') Do (Set "FP=%%B"
        Call :Sub "%%FP:/=\%%")
:EndIt
Echo( Press any key to exit...
Timeout -1 1>Nul
Exit/B
        :Sub
        If Exist "%PF%%~1\" (Set PP="%PF%%~1") Else (Set PP="%~1")
        Echo=%PP%

Post Reply