Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C++ trying to figure out how to clip file extensions for project  (Read 9792 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
I have a project I am working on in C++ using VC++ 6 and I am reading in a text file that is output from a batch that runs a series of dir/b *.extension>>list.txt routines to append to file all file extensions I want to have in my list file instead of just all files in that directory. This is used to list all the movie/videos to this text file. The C++ is used to create/compile a HTML page for my HTPC.

Currently I am able to open the list.txt file and add each line to the creation of an HTML page that places that lines contents as a hyperlink in an alphabetical listing, but instead of listing it as say movie1.mp4, I want to list it as movie1. So I was thinking that if I trimmed 4 characters off of the line, that would solve it, but for some files with 4 character file extensions instead of 3 character file extensions I would then have to trim 5 characters to include the .mp4 or .mpeg to remove the dot as well.

So I was thinking that maybe I can read in the line, then run a process that looks for the dot "." and it doesnt matter then how short or long a file extension is. Only problem is I have never done this before and so I have stumped myself. Did some searching in google and figured I'd post here since I didnt find anything close to what I am trying to accomplish.

In the end the C++ program writes/compiles a htpc.htm webpage that I have linked to the startup of the htpc, so I can pick movies and music etc from an organized menu. I was editing this htpc.htm page manually and thought that I should really just write a program that will import file names and then link them in a html webpage vs having to go in and add the additional line into my html page each time I add new videos and music etc.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: C++ trying to figure out how to clip file extensions for project
« Reply #1 on: August 08, 2011, 07:46:03 PM »
Why have the batch file to begin with?
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: C++ trying to figure out how to clip file extensions for project
« Reply #2 on: August 08, 2011, 08:29:30 PM »
Hello BC... well I have the batch to begin with mainly because I am using the dir/b to get directory listing. I could have snuck that into the C++ with a system("dir/b *.mp4>>list.txt"); type call but been staying away from using system calls as for I read that they are a bad habit to use in C++. So I figured, I'd run a quick batch, then have my list.txt file ready for me with the files I want to have included in the created HTPC webpage and then use the C++ to then read in this text file and run with it.

*If you can show me a technique to output a file list to a file avoiding the system call in C++, I would be very interested in seeing how this is done. I am sure it can probably be done, but this is new territory accessing a file system to then pass that information (filename.extension) listing to a file vs using DOS commands.

 I have posted a very basic draft of what I have. This isnt the web page I am currently using, just a quick proof of concept sort of test, which generates the HTPC.htm page. HTML is easy to make pretty later on with frames and tables etc later on. I also need to add code to delete the HTPC.htm file if it exists prior to writing to it -or- clear the file first to avoid a double appending of HTML code added to the end of the initial. Deleting it I would have to use a system call, but I might be able to wipe its contents using C++ that I will have to figure out how to pass nothing to it to overwrite something with nothing..lol  then be able to write cleanly to it. I am guessing that i can write overtop of it with a single space " " probably since if it appends the <HTML> after a single space, the webpage will still act the same as the browser translates it.

Being able to trim off .mp4 and .mpeg from the name of the file listed in the html hyperlink displayed text would be neat to do to clean this up I figured since I just want titles listed and the file entension showing is kind of ugly. I figured there must be a way to find "." and trim "." and the next remaining characters no matter how many there are on that line without affecting the next line below it. But still stumped :-\


Here is test contents of link.txt
Code: [Select]
file1.mp4
file2.mp4
file3.mp4
file4.mp4
file5.mp4



And here is my current code, which may look a little ugly without comments and I merged a book reference and online reference of code for technique and then tweaked them together to get what I needed since I am not good at just coding at this level without looking in my books or online etc unless I want to battle it out when compiling and fixing the issues.

Code: [Select]
#include<iostream>

using std::cerr;
using std::cout;
using std::endl;

#include<fstream>
using namespace std;
using std::ifstream;

#include<cstdlib>


int main()
{
ifstream indata;
char buffer1[2048];
indata.open("link.txt");
if(!indata){
cerr<<"Error: file could not be opened"<<endl;
exit(1);
}
indata>>buffer1;

ofstream myfile1;
myfile1.open ("htpc.htm",ios::out|ios::app);

myfile1<<"<html>"<<endl;
myfile1<<"<body>"<<endl;
myfile1<<"<center>"<<endl;
myfile1.close();
while(!indata.eof()){
ofstream myfile2;
myfile2.open ("htpc.htm",ios::out|ios::app);

myfile2<<"<a href\""<<buffer1<<"\">"<<buffer1<<"</a><BR>"<<endl;

indata>>buffer1;
myfile2.close();
}
ofstream myfile3;
myfile3.open ("htpc.htm",ios::out|ios::app);
myfile3<<"</body>"<<endl;
myfile3<<"</html>"<<endl;

myfile3.close();




indata.close();
cout<<"End-of-file reached.."<<endl;
return(0);
}

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: C++ trying to figure out how to clip file extensions for project
« Reply #3 on: August 08, 2011, 10:16:48 PM »
Code: [Select]
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//TASK: enumerates all MP3 files in a given directory and creates a simple HTML file with links for each one.
int _tmain(int argc, _TCHAR* argv[])
{
WIN32_FIND_DATA* fdata = (WIN32_FIND_DATA*)malloc(sizeof(WIN32_FIND_DATA));

//Change this as needed to use a different folder.
//change this to change the folder. Possibly make command line argument, who knows.
//make sure to change the HREF line below if you change this, too.
HANDLE fhandle = FindFirstFile(L"D:\\music\\*.mp3",fdata);
BOOL filesfound=TRUE;
if(fhandle==INVALID_HANDLE_VALUE)
{
cout << GetLastError();
filesfound=FALSE;

}
ofstream htmlout;
//delete the file if it exists....
DeleteFile(L"D:\\htpc.htm");
htmlout.open ("D:\\htpc.htm",ios::out|ios::app);
htmlout<< "<html><body><center>";
if(!filesfound) htmlout << "<h2>No Files.</h2>";
if(fhandle!=INVALID_HANDLE_VALUE)
{
for(;;)
{
//assumes fdata->cFileName is a WCHAR_T.
char buff[MAX_PATH];
//wcstombs ( char * mbstr, const wchar_t * wcstr, size_t max );
ZeroMemory(buff,MAX_PATH);
wcstombs(buff,fdata->cFileName,MAX_PATH);
string outputname;
outputname.assign(buff);
cout << "found file:" ;
cout << buff << endl;

int dotpos = outputname.rfind(".");
string titlename = outputname.substr(0,dotpos);

cout << "name:" << titlename;
htmlout << "<a href=\"" << "D:\\music\\" << outputname << "\">" << titlename << " </a><br>\n";
ZeroMemory(fdata,sizeof(WIN32_FIND_DATA));
BOOL fnf=FindNextFile(fhandle,fdata);
if(fnf==0) break;
}
}
htmlout << "</center></body></html>";
FindClose(fhandle);
htmlout.close();
//int test;
//cin>>test;

free(fdata);
return 0;
}


I was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: C++ trying to figure out how to clip file extensions for project
« Reply #4 on: August 08, 2011, 11:54:47 PM »
Dave, why don't you just make the original batch file strip off the extensions? And build the html page too? Isn't writing a C++ program to do this using a sledgehammer to crack a nut?

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: C++ trying to figure out how to clip file extensions for project
« Reply #5 on: August 09, 2011, 01:29:39 PM »
Thanks BC for posting the code, I am going to copy/paste and compile and look through it to learn how it works. And may post questions later. Really interested in how it addresses the file system to retreive the info that the dir/b does in that batch.

Hello Salmon.... well the situation I have is that I know some of both batch and C++ with C++ being my stronger skill when dealing with reading/writing from and to files and dealing with variables and strings and am a master of neither, so I end up making lots of ugly mixed program/scripts to get the job done usually.

I guess I went the route of C++ also because I know that batch has limitations and C++ can do all. I mainly didnt know how to perform a process similar to dir/b to access a file system to retrieve file names and figured the batch was the quick band aid to create the information that I can then feed to the C++ program.

*If you have a moment to show how this can be done in batch that would be great. As for I am interested in learning how it can be done by example. It would be neat to see how to pass the contents of dir/b into variables or read in the listing from dir/b that was written to list.txt at the start of the batch to then output that to an html file as well as trimming off the file extensions. I am guessing its mainly just creation of the html file initially by a bunch of
Quote
@echo. <HTML><BODY>>>htpc.htm
  although I am not sure if escape characters would be needed to not confuse the batch with the < and >  tags of the html, and the planting of the information in a looped process that performs line creation until eof of something similar to
Quote
@echo. <a href"(variable [A] here to pass full filename and extension)">(variable here to pass the full file name without extension) [/url]>>htpc.htm
/a instead of /url above within < > instead of [ ] ... this is being translated in this page

LOL at using a sledgehammer to crack a nut... yah I guess that is a good analogy to what I was doing! I suppose I was using a sledgehammer because it will squash the nut, while I was unsure if batch was going to be like hitting the nut with a butter knife..lol ... Not to bash batches ability to get the job done, but I guess just using an overkill tool to get the job done because I was sure C++ would do it and unsure as to if batch could do it on its own without running into a limitation.  ;D

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: C++ trying to figure out how to clip file extensions for project
« Reply #6 on: August 09, 2011, 01:52:18 PM »
I came up with this but it's useless and doesn't work for some reason.
Code: [Select]
REM output HTML file:
setolocal enabledelayedexpansion
SET outhtml=D:\htpc.html
SET searchpath=D:\music\

echo ^<html^>^<body^>^<center^> > %outhtml%

for /f "delims=*" %%P in ('dir /b %searchpath%*.mp3') do
(
echo "%%P"
call :separate %%P
echo ^<A href="!searchpath!%%P"^>%FILE%^</a^> >> %outhtml%

)
echo ^</center^>^</body^>^</html^> >>%outhtml%
goto EOF
:separate
set FILE=%~n1
set EXT=%~x1
exit /b 0
:EOF
I was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: C++ trying to figure out how to clip file extensions for project
« Reply #7 on: August 09, 2011, 02:23:50 PM »
You should stick with whatever you feel comfortable with... I prefer batch or other scripting languages (especially Powershell which (I don't personally think) has such a steep learning curve as some people make out) because you can run the thing and see if it works without having to compile it first. A thought: I expect you "normalise" your file names first so they don't look like this "Nightmare.on.Elm.Street.[XVID].REPACK.Superfly.mp4" where I guess you would isolate the extension by counting backwards from the end of the string until you hit a dot. (of course you wouldn't have files like that would you???!!! Because we're talking about fair-use format shifting of stuff we own. Of course.

Anyhow Windows batch has "variable modifiers" (see the FOR help accessible via FOR /? for details) to help you get at file properties. If %%A is a file name then %%~dA is the drive letter (with colon) and %%~pA is the path (folder), %%~nA is the name part and %%~xA is the extension (with dot) so if you did this you would echo just the file names

FOR /F "delims=" %%A in ('dir /b *.mp4') do (
    echo %%~nA
    )

You can echo characters like < and > if you escape them with carets like this ^< so you could make a simple html file like this

FOR /F "delims=" %%A in ('dir /b *.mp4') do (
    echo %%~nA^</br^> >> mymenu.html
    )

Code: [Select]
S:\Test\movie list>dir
 Volume in drive S is USB-1
 Volume Serial Number is 2C51-AA7F

 Directory of S:\Test\movie list

09/08/2011  21:16    <DIR>          .
09/08/2011  21:16    <DIR>          ..
06/06/2011  18:56       185,665,722 1307293768080.mp4
07/06/2011  18:36       168,581,204 1307401963926.mp4
14/06/2011  17:41       174,549,670 1308008220765.mp4
22/06/2011  20:40       401,292,105 1308705903296.mp4
09/08/2011  21:16               289 make-html.bat
17/05/2011  00:49       172,288,053 Mapa sonoro Antonia Font 16-05-11.mp4
09/08/2011  21:16             1,036 mymenu.html
07/05/2011  21:08       331,807,998 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_10_b010w7m9_default.mp4
03/04/2011  09:55       313,634,325 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_1_b01075w0_default.mp4
03/04/2011  10:00       332,694,729 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_2_b01083xt_default.mp4
10/04/2011  10:32       321,423,288 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_3_b0109bll_default.mp4
10/04/2011  10:41       329,109,128 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_4_b0109bln_default.mp4
17/04/2011  15:46       334,111,133 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_5_b010j4ms_default.mp4
17/04/2011  15:51       348,788,609 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_6_b010j4mv_default.mp4
30/04/2011  21:23       327,717,522 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_7_b010p4x2_default.mp4
30/04/2011  21:27       323,716,560 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_8_b010p4x4_default.mp4
07/05/2011  21:05       314,839,606 Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_9_b010w7m7_default.mp4
17/05/2011  00:49       172,288,053 test.mp4
              18 File(s)  4,552,509,030 bytes
               2 Dir(s)  237,718,470,656 bytes free

Code: [Select]
@echo off
echo ^<html^> > mymenu.html
echo ^<body style="font-family:Arial; font-size:18px;margin-left:50px"^> >> mymenu.html
echo Movie List^</br^> >> mymenu.html
FOR /F "delims=" %%A in ('dir /b *.mp4') do (
    echo %%~nA^</br^> >> mymenu.html
    )
echo ^</html^> >> mymenu.html
   

Code: [Select]
S:\Test\movie list>type mymenu.html
<html>
<body style="font-family:Arial; font-size:18px;margin-left:50px">
Movie List</br>
1307293768080</br>
1307401963926</br>
1308008220765</br>
1308705903296</br>
Mapa sonoro Antonia Font 16-05-11</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_10_b010w7m9_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_1_b01075w0_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_2_b01083xt_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_3_b0109bll_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_4_b0109bln_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_5_b010j4ms_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_6_b010j4mv_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_7_b010p4x2_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_8_b010p4x4_default</br>
Spiral_Series_3_-_The_Butcher_of_La_Villette_Episode_9_b010w7m7_default</br>
test</br>
</html>





Salmon Trout

  • Guest
Re: C++ trying to figure out how to clip file extensions for project
« Reply #8 on: August 09, 2011, 04:04:38 PM »
I came up with this but it's useless and doesn't work for some reason.

Code: [Select]
'setolocal' is not recognized as an internal or external command,
operable program or batch file.

No comment...

Code: [Select]
The syntax of the command is incorrect.
S:\Test\movie list>for /f "delims=*" %P in ('dir /b S:\Test\movie list*.mp4') do

You want the open parenthesis on the same line as the DO

Code: [Select]
REM output HTML file:

REM not needed!
setlocal enabledelayedexpansion

SET outhtml=D:\htpc.html

REM I changed this to suit my test folder
SET searchpath=%cd%

echo ^<html^>^<body^>^<center^> > %outhtml%

REM OK now I ut a backslash in
for /f "delims=*" %%P in ('dir /b "%searchpath%\*.mp4"') do (
echo "%%P"

REM Colon is not obligatory
REM You can do this inline anyhow
call separate %%P
echo ^<A href="%searchpath%%%P"^>%FILE%^</a^> >> %outhtml%
)
echo ^</center^>^</body^>^</html^> >>%outhtml%

REM Gotta skip over the sub somehow
goto end


:separate
set FILE=%~n1
set EXT=%~x1

REM This is how you get out of the sub
REM There isn't actually a label called :EOF
REM This means RETURN
REM This colon is obligatory
goto :EOF

:end
REM We're done



Code: [Select]
<html><body><center>
<A href="S:\Test\movie list1307293768080.mp4"></a>
<A href="S:\Test\movie list1307401963926.mp4"></a>
<A href="S:\Test\movie list1308008220765.mp4"></a>
<A href="S:\Test\movie list1308705903296.mp4"></a>
<A href="S:\Test\movie listMapa sonoro Antonia Font 16-05-11.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_10_b010w7m9_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_1_b01075w0_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_2_b01083xt_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_3_b0109bll_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_4_b0109bln_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_5_b010j4ms_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_6_b010j4mv_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_7_b010p4x2_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_8_b010p4x4_default.mp4"></a>
<A href="S:\Test\movie listSpiral_Series_3_-_The_Butcher_of_La_Villette_Episode_9_b010w7m7_default.mp4"></a>
<A href="S:\Test\movie listtest.mp4"></a>
</center></body></html>

but it doesn't work in my browser...







DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: C++ trying to figure out how to clip file extensions for project
« Reply #9 on: August 09, 2011, 05:03:04 PM »
Thanks Salmon for showing me how to do that in batch. I have gone in and changed my file names manually for many of the MP4's since some were created as DVD_unknown.mp4 for example and then I have to go in and type Cheech_And_Chong__Things_Are_Tough_All_ Over.mp4 and switch like Terminator4x3.mp4 to just Terminator.mp4. I am using Format Factory to generate many of my MP4's off of my DVD collection and it worked really well for about 95% of my movies. Other DVD's that don't like it I can use my capture card and software in the HTPC to act like a DVR to play to the HTPC from my DVD player and create the MP4 the long way for my home theater setup. The DVD's and VHS tapes then go into storage and I have my entire movie collection on a 500GB drive which is currently about 200GB. My wife the one day said oh, well since you converted those movies we can now get rid of them and sell them... I SAID NOOOO WAY!!! Its Piracy if we dont retain ownership of the movies! So we legally have to keep physical posession of them for as long as we own the digital copy of it. She agreed then to let the box of movies stay in the closet untampered with..lol   So I am only creating "Fair-Use" Format Shifted Copies! Most movies are about 700MB to 1.2GB in size created as 720 format. Since I only have a 32" screen 720 is fine.

Since I want to know how to do it in both Batch & C++ now, and have some compile errors in C++, I am going to continue this thread with the errors I got with BC's code to figure out why I am having compile issues. It might be as simple as my compiler is different than BC's.

I ran into a problem years ago when i was writing C++ in both Borland 4.0 that I owned which came bundled with a Borland book, and MS VC++ 5.0 that the college owned at the time in which one liked the header or handler I think its called #include<iostream> and the other preferred #include<iostream.h>...so i tried to remove the .h from #include<windows.h> since the rest were without .h and got a single error of windows not found or something along those lines. Also went and added .h to those without.h to make all the same statement style and it then gave me like 13 errors instead of 4 compiling. So below is the error I get that maybe you can point me in the right direction to resolve since it doesnt look very straight forward to me as a error of missing a ; etc would be.

I have never used the Windows.h header/handler before so maybe I need to do something other than just copy/paste and compile. In the past with other programs calling out to custom .h files, I remember having to go in and build .h files to be present for the compilation when it links it to the main program. I have never written my own .h file to link to and mainly have coded within the console programming C/C++ environment as taught in college. I kind of feel like I was passed through the system when it comes to C++ programming as for in advanced C++ we were only dealing with CLASSES. Leaving college with a Computer Systems Management (MIS) Degree and favoring C++ over other languages and digging into the more advanced stuff, i feel as if they should have covered classes when I took the 2nd course Intermediate C++, then really dove into the advanced stuff in the advanced C++ the 3rd semester to go into stuff like what BC has coded here that is new territory and I am soaking up as much info as I can from it. The other thing I need to do is stay active with it too instead of just coding when I need something, since if you dont use it you lose it. Similar to how I passed advanced math with calculus and loved trig, and when going to assist a friend with his math the one day after not doing it for almost 10 years, I knew some stuff right off, but other stuff I was like hmm...lets look it up..lol If I played with it after learning it, I would have retained it better and programming is the same way I feel.
Quote
--------------------Configuration: htpc3 - Win32 Debug--------------------
Compiling...
htpc3.cpp
c:\dchtml\htpc3.cpp(7) : error C2061: syntax error : identifier '_TCHAR'
c:\dchtml\htpc3.cpp(14) : error C2664: 'FindFirstFileA' : cannot convert parameter 1 from 'unsigned short [15]' to 'const char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\dchtml\htpc3.cpp(24) : error C2664: 'DeleteFileA' : cannot convert parameter 1 from 'unsigned short [12]' to 'const char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\dchtml\htpc3.cpp(36) : error C2664: 'wcstombs' : cannot convert parameter 2 from 'char [260]' to 'const unsigned short *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

htpc3.obj - 4 error(s), 0 warning(s)

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: C++ trying to figure out how to clip file extensions for project
« Reply #10 on: August 09, 2011, 05:50:23 PM »
Compiles in VC++ 2008.

Your problem (assuming it isn't also related to compiler versions) is that your defs are set to use ANSI rather than Unicode. I think that is the default for earlier versions of the MS compiler. This breaks the assumption that fdata->cFileName is a WCHAR_T, as well as others.

enter #define UNICODE before the windows.h include.

Quote
The other thing I need to do is stay active with it too instead of just coding when I need something, since if you dont use it you lose it.
I hardly ever write C++ code, personally. I know  what you mean but I don't think it applies as strongly as you think. There is certainly no reason to shoehorn projects into a C++ mould.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: C++ trying to figure out how to clip file extensions for project
« Reply #11 on: August 09, 2011, 06:52:28 PM »
Thanks BC I'll give that a try on my VC++ 6.0... I figured it was compiler version related. Never knew about the ANSI vs Unicode change.

I have been tempted to install a newer version of MS C++ such as 2008 Express which came bundled with my VC++ 2008 Book, but the clunker workhorse laptop I use while on breaks at work is a Pentium III 600Mhz with 384MB Ram running clean unpatched oem install of XP Pro SP2, and patching it to SP3 and all updates for a laptop that remains as offline usage brings it to a horrible crawl, so I figured going through installing 2008 and Dot NET etc might be too much for it to handle. I have VC++ 2008, VB 2008, and C# 2008 installed on my desktop home computer and played with them some. Only complaint I have with them is that whatever you write you need to make sure that Dot NET 3.5 I think the version is is installed on any system that you execute the compiled programs from. Its too bad they cant squeeze that into the .EXE so it can be stand alone I guess you could call it vs requiring the 3.5 framework that most systems I have seen dont normally update to with MS updates. I think the MS updates bring systems to Dot NET 2.0.

Salmon... I tried your batch and tweaked it some to work on my system which is executing it on C: instead of D: and the problem I am seeing same as what your output shows is that the instruction to pass %FILE% into the created HTML line that is written to HTPC.htm seems like its not getting populated with the File Name information.

For some reason, %FILE% in the line below isnt inherriting the file name from set FILE=%~n1 so because of this the page loads as an empty page with no text linked when you load the htpc.htm page.

Code: [Select]
echo ^<A href="%searchpath%%%P"^>%FILE%^</a^> >> %outhtml%Here is what is created from the batch when changing SET outhtml=D:\htpc.html to SET outhtml=C:\htpc.html since my D: drive is my DVD drive. I also have to change SET searchpath=%cd% to SET searchpath=%cc% , which was a guess on my part that the "d" meant D: drive and so the last "c" would be C: drive, and that gives me the output of

Code: [Select]
<html><body><center>
<A href="test1.mp4"></a>
<A href="test2.mp4"></a>
<A href="test3.mp4"></a>
</center></body></html> 

Instead of

Code: [Select]
<html><body><center>
</center></body></html>

so I end up with what you get...so still stuck at %FILE% not populating with the File Name without the extension portion. Once this file name issue is fixed for populating %FILE% we should end up with


Code: [Select]
<html><body><center>
<A href="test1.mp4">test1</a>
<A href="test2.mp4">test2</a>
<A href="test3.mp4">test3</a>
</center></body></html>

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: C++ trying to figure out how to clip file extensions for project
« Reply #12 on: August 09, 2011, 07:23:28 PM »
the batch you used  there was the one I originally created, with his modifications. As he noted, it doesn't work. I don't think it is worth fixing since he already offered a batch solution.


Only complaint I have with them is that whatever you write you need to make sure that Dot NET 3.5 I think the version is is installed on any system that you execute the compiled programs from. Its too bad they cant squeeze that into the .EXE so it can be stand alone I guess you could call it vs requiring the 3.5 framework that most systems I have seen dont normally update to with MS updates. I think the MS updates bring systems to Dot NET 2.0.
And yet, nobody seems to consider the necessity of a JVM and the Java class libraries being installed to run java programs as a point against Java development. Almost every Development tool will require something of the host machine in the way of libraries for any non-trivial project. All Versions of Visual Studio .NET allow you to target a different framework version. You can create programs that target .NET 2.0 in Visual Studio 2008. I don't, because there is no point. If somebody doesn't have .NET 3.5 installed, they can install it. I'm not about to do more work getting things working with .NET 2.0 just so somebody can be lazy; additionally, Windows Vista and 7 come with .NET 3.5 anyway, and I don't make a habit of targeting 10 year old Operating Systems.

Also of interest: if you do it right, you can create executables that run on both Windows, OSX, and Linux using .NET. Which is an advantage.


I was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: C++ trying to figure out how to clip file extensions for project
« Reply #13 on: August 10, 2011, 12:14:11 AM »
I also have to change SET searchpath=%cd% to SET searchpath=%cc% , which was a guess on my part

%cd% is a special Windows-supplied variable - it holds the drive letter and path of the current directory at the time the code is run. %cc% will presumably just be blank. Anyhow as BC_P said, I was just playing around with his code. You could try the code in my post, the one which starts "You should stick with whatever you feel comfortable with..." but on that note, it may be that going into batch was a step in the wrong direction, and I apologise for hijacking the thread to no good purpose.




« Last Edit: August 10, 2011, 01:14:26 AM by Salmon Trout »

Salmon Trout

  • Guest
Re: C++ trying to figure out how to clip file extensions for project
« Reply #14 on: August 10, 2011, 01:13:32 AM »
So I figured, I'd run a quick batch, then have my list.txt file ready for me with the files I want to have included in the created HTPC webpage and then use the C++ to then read in this text file and run with it.

You could just modify the batch you run first. Instead of this...

dir /b *.mp4 >> list.txt

...you could use this, which does the same thing except it strips off the .mp4 extension:

for /f "delims=" %%A in ('dir /b *.mp4') do echo %%~nA >> list.txt



Salmon Trout

  • Guest
Re: C++ trying to figure out how to clip file extensions for project
« Reply #15 on: August 10, 2011, 01:14:51 AM »
..

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: C++ trying to figure out how to clip file extensions for project
« Reply #16 on: August 14, 2011, 04:01:44 PM »
Thanks Salmon, and no apology needed... I dont feel that it was hijacked..lol   I welcome all input on the matter!

Thanks for showing me how to strip off the file extension with

Code: [Select]
for /f "delims=" %%A in ('dir /b *.mp4') do echo %%~nA >> list.txt
And clarifying %cd% vs my assumption of %cc%

Khasiar



    Intermediate

    Re: C++ trying to figure out how to clip file extensions for project
    « Reply #17 on: September 15, 2011, 11:19:10 PM »
    here's another idea, im going to skip the error checking


    ifstream inFile;
    string temp;
    vector<string> fileNames, newFileNames;



    system("dir /s /b *.mp4 *.avi *.mpeg >> out.txt");
    inFile.open("out.txt");

    while(inFile){

       getline(inFile, temp);
       fileNames.push_back(temp);

    }//while

    inFile.close();

    for (int i = 0; i < fileNames.size(); i++){

       for (int i2 = fileNames.at(i).size() - 1; i2 >= 0 ; i2--)
          if (fileNames.at(i).at(i2) == '.'){
             newFilesNames.push_back(fileNames.at(i).substr(0, (fileNames.at(i).size() - (fileNames.at(i).size() - i2 ))
             i2 = -1

          }//if


    }//for

    then youd probably want to rename the file

    string temp, cmd= "rename ";

    for (int i = 0; i < newFileNames.size(); i++){

    temp = cmd + fileNames.at(i) + " " + newFileNames.at(i);
    system(temp.c_str());

    }//for

    something along these lines, didnt really get to test it but ive done a similar project before